1

I'm trying to upload a file from android but I'm getting an exception SocketTimeoutException. I have tried use curl and upload the same file and it works.

I have tried with differents timeouts: 20,30,50,100 seconds but exception comes on the first 10 seconds.

Source code

MediaType OCTET_STREAM = MediaType.parse("application/octet-stream");

        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(20,TimeUnit.SECONDS);
        client.setReadTimeout(20, TimeUnit.SECONDS);
        FileInputStream fis = new FileInputStream(localfile_path);
        byte[] content = IOUtils.toByteArray(fis);
        RequestBody body = RequestBody.create(OCTET_STREAM, content);

        JSONObject dropbox_json = new JSONObject();
        try {
            dropbox_json.put("path", dropbox_path);
            dropbox_json.put("mode", mode);
            dropbox_json.put("autorename", autorename);
            dropbox_json.put("mute", mute);
        }
        catch(JSONException e){}

        String dropbox_arg = dropbox_json.toString();

        Request request = new Request.Builder()
                .url("https://content.dropboxapi.com/2/files/upload")
                .addHeader("Authorization", "Bearer "+MyGlobals.getDropboxToken())
                .addHeader("Dropbox-API-Arg", dropbox_arg)
                .addHeader("data-binary", "@"+localfile_path)
                .post(body)
                .build();

        Call call = client.newCall(request);
        com.squareup.okhttp.Response response = call.execute();
        return response.body().string();

Exception

11-08 21:34:42.426 29236-31493/es.ric.android.swipe W/System.err: java.net.SocketTimeoutException: timeout
11-08 21:34:42.449 29236-31493/es.ric.android.swipe W/System.err:     at okio.Okio$3.newTimeoutException(Okio.java:207)
11-08 21:34:42.464 29236-31493/es.ric.android.swipe W/System.err:     at okio.AsyncTimeout.exit(AsyncTimeout.java:261)
11-08 21:34:42.478 29236-31493/es.ric.android.swipe W/System.err:     at okio.AsyncTimeout$1.write(AsyncTimeout.java:158)
11-08 21:34:42.495 29236-31493/es.ric.android.swipe W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
11-08 21:34:42.510 29236-31493/es.ric.android.swipe W/System.err:     at okio.RealBufferedSink.write(RealBufferedSink.java:46)
11-08 21:34:42.531 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:302)
11-08 21:34:42.547 29236-31493/es.ric.android.swipe W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
11-08 21:34:42.565 29236-31493/es.ric.android.swipe W/System.err:     at okio.RealBufferedSink.write(RealBufferedSink.java:96)
11-08 21:34:42.595 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.RequestBody$2.writeTo(RequestBody.java:96)
11-08 21:34:42.613 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:887)
11-08 21:34:42.647 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)
11-08 21:34:42.667 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.Call.getResponse(Call.java:268)
11-08 21:34:42.687 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
11-08 21:34:42.706 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
11-08 21:34:42.719 29236-31493/es.ric.android.swipe W/System.err:     at com.squareup.okhttp.Call.execute(Call.java:79)
11-08 21:34:42.735 29236-31493/es.ric.android.swipe W/System.err:     at es.ric.android.swipe.services.ServiceSaveFoto.upload(ServiceSaveFoto.java:134)
11-08 21:34:42.751 29236-31493/es.ric.android.swipe W/System.err:     at es.ric.android.swipe.services.ServiceSaveFoto.onHandleIntent(ServiceSaveFoto.java:64)
11-08 21:34:42.768 29236-31493/es.ric.android.swipe W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-08 21:34:42.782 29236-31493/es.ric.android.swipe W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-08 21:34:42.796 29236-31493/es.ric.android.swipe W/System.err:     at android.os.Looper.loop(Looper.java:135)
11-08 21:34:42.809 29236-31493/es.ric.android.swipe W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:61)
11-08 21:34:42.823 29236-31493/es.ric.android.swipe W/System.err: Caused by: java.net.SocketException: socket is closed
11-08 21:34:42.928 29236-31493/es.ric.android.swipe W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:759)
11-08 21:34:42.942 29236-31493/es.ric.android.swipe W/System.err:     at okio.Okio$1.write(Okio.java:80)
11-08 21:34:42.955 29236-31493/es.ric.android.swipe W/System.err:     at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
11-08 21:34:42.961 29236-31493/es.ric.android.swipe W/System.err:   ... 18 more

1 Answer 1

5

Looks like you need a write timeout also.

client.setWriteTimeout(20, TimeUnit.SECONDS);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.