在android中发送多方或JSON数据时改进SocketTimeOutException

前端之家收集整理的这篇文章主要介绍了在android中发送多方或JSON数据时改进SocketTimeOutException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通过改造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)

我试图在Request Adapter中增加请求的超时

private OkHttpClient getClient() {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(30,TimeUnit.SECONDS);
        client.setReadTimeout(30,TimeUnit.SECONDS);
        return client;
    }

解决方法

我几天前有这个错误,我发现你的解决方案是正确的,但增加了更多的时间
private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(5,TimeUnit.MINUTES);
    client.setReadTimeout(5,TimeUnit.MINUTES);
    return client;
}

对于OkHttp3

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient.Builder()
    .connectTimeout(5,TimeUnit.MINUTES)
    .readTimeout(5,TimeUnit.MINUTES)
    .build();
    return client;
}
原文链接:https://www.f2er.com/android/317413.html

猜你在找的Android相关文章