通过改造lib发送Mutipart或JSON数据时遇到问题
改造界面
@Multipart
@POST("/api/v1/protected/updateprofile")
void uploadPhoto(@Part("name") String name,
@Part("image") TypedFile file,
Callback<ApiResponseModel> callback);
适配器代码
private RestApi restApi;
RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(getClient())).setConverter(new GsonConverter(gson)).setRequestInterceptor(interceptor).setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(Config.baseUrl).build();
restApi = restAdapter.create(RestApi.class);
发送JSON请求后生成的错误日志
D/Retrofit﹕ ---- ERROR http://www.example.com/api/v1/protected/addfriends
11-05 11:22:18.594 13384-15325/com.veddislabs.plicx D/Retrofit﹕ java.io.InterruptedIOException: timeout
at okio.AsyncTimeout.exit(AsyncTimeout.java:258)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:206)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:153)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.net.SocketException: Socket closed
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:136)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at okio.Okio$2.read(Okio.java:136)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:206)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:153)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
我试图在请求适配器
中增加请求的超时 private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
return client;
}
答案 0 :(得分:21)
我几天前有这个错误,我发现你的解决方案是正确的,但增加了更多的时间
private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.MINUTES);
client.setReadTimeout(5, TimeUnit.MINUTES);
return client;
}
private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
return client;
}
答案 1 :(得分:0)
对我来说问题来自服务器端。
我必须通过更改API服务器上的tcp_timestamp
配置来禁用sysctl
。此外,如果您在同一台计算机上使用其他服务(即Redis等),则应注意这对它们的影响。
在Github上的issue找到了这个。