Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit SocketTimeOutException in sending multiparty or JSON data in android

Facing problem in sending a Mutipart or JSON data through retrofit lib

Retrofit Interface

@Multipart
@POST("/api/v1/protected/updateprofile")
void uploadPhoto(@Part("name") String name,
                 @Part("image") TypedFile file,
                 Callback<ApiResponseModel> callback);

Adapter Code

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);

Error Log Generated after sending JSON request

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)

I have tried to increase the timeout of the request in Request Adapter

  private OkHttpClient getClient() {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(30, TimeUnit.SECONDS);
        client.setReadTimeout(30, TimeUnit.SECONDS);
        return client;
    }
like image 576
Dharmendra Pratap Singh Avatar asked Nov 05 '14 06:11

Dharmendra Pratap Singh


1 Answers

I have this error a few days ago and I discovered that your solution is correct but add more time

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

For OkHttp3

 private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient.Builder()
    .connectTimeout(5, TimeUnit.MINUTES)
    .readTimeout(5, TimeUnit.MINUTES)
    .build();
    return client;
}
like image 193
kroky Avatar answered Oct 07 '22 13:10

kroky