我只是在没有OkHttp的情况下使用了Retrofit。 这是我的代码
private void executeForm(String pubToken,String acctToken,Integer Amt,String UID){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(" https://us-central1-deyapay-192704.cloudfunctions.net/plaidCredentialsAddMoney/")
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
APIService apiservice=retrofit.create(APIService.class);
Call<PostData> call=apiservice.achPost(pubToken,acctToken,Amt,UID);
call.enqueue(new Callback<PostData>() {
@Override
public void onResponse(Call<PostData> call, Response<PostData> response) {
if(response.isSuccessful()){
response.body();
String dp = response.body().getToken();
Log.d(TAG,dp);
Toast.makeText(ACH.this,"success",Toast.LENGTH_SHORT).show();
}
else
{
}
}
@Override
public void onFailure(Call<PostData> call, Throwable t) {
t.printStackTrace();
Log.e(TAG,t.toString());
}
});
}
我通过研究错误添加了OkHttp3。这是处理OkHttp3的部分
OkHttpClient OClient = new OkHttpClient.Builder()
.readTimeout(60,TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(" https://us-central1-deyapay-192704.cloudfunctions.net/plaidCredentialsAddMoney/")
.client(OClient)
.addConverterFactory(GsonConverterFactory.create(gson));
Retrofit retrofit = builder.build();
但是我得到了同样的错误。我认为没有必要为错误添加OkHttp3,因为我在使用Retrofit时没有使用OkHttp然后按预期工作。 这是日志输出
03-24 16:16:45.591 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: java.net.SocketTimeoutException: timeout
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:593)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:601)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:146)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-24 16:16:45.592 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay W/System.err: at java.lang.Thread.run(Thread.java:818)
03-24 16:16:45.593 28361-28361/com.bodaty.deyaPay.deyaPay E/MyActivity: java.net.SocketTimeoutException: timeout
答案 0 :(得分:2)
尝试设置writeTimeout()
:
OkHttpClient OClient = new OkHttpClient.Builder();
OClient.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES);