5

I am trying to upload a JPEG image file from an Android device. I am using the square/okhttp library for creating a request. I am facing this issue on the Lenovo Yoga tablet, that when I am trying to upload the image, it gets the below exception. But when I run the same code on Samsung Galaxy Tab 10", it all works fine and the image gets uploaded successfully.

The camera captures the image and stores it at /storage/emulated/0/ from where the app will pick up the image and try to upload it. I have a service running in the background that does the upload.

final MediaType MEDIA_TYPE_IMAGE_JPEG = MediaType.parse("image/jpeg");

        File file = new File(path);
        Request request = new Request.Builder()
                .url(baseUrl)
                .addHeader("timestamp", map.get("timestamp"))
                .addHeader("Content-Type", map.get("Content-Type"))
                .post(RequestBody.create(MEDIA_TYPE_IMAGE_JPEG, file))
                .build();

        OkHttpClient client = new OkHttpClient();
        Response response = client.newCall(request).execute();

Exception:

java.net.SocketTimeoutException: timeout
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2
Stream.java:593)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:601)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:146)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.394 1770-2339/commyapp.app.debug 
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
  W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
  11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
  W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)
3
  • Timeout based exceptions are quite unpredictable. It does not entirely depend on the hardware, but also on internal configurations as well. Commented Nov 27, 2017 at 5:17
  • @HiteshPamnani: which internal configurations you mean ? Commented Nov 27, 2017 at 5:31
  • 1
    I meant the same thing as Prokash Sarkar was trying to point out here. Internal configurations meant the network settings for each device. Commented Nov 27, 2017 at 6:26

1 Answer 1

11

You have to increase the read/write timeout on the server end. A temporary workaround for Android would be increasing the default timeout for OkHttp3 client:

OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(120, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .build();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it worked. Its strange that it was working on some devices and not others. But setting the timeout value surely did work.
Exceptions for timeout is unpredictable. Some device may process the network call faster while others will take longer time. It's safe to put some extra time on network client so that you don't get run out of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.