Android okhttp-Dropbox http API v2

Android okhttp-Dropbox http API v2,android,dropbox,Android,Dropbox,我正试图从android上传一个文件,但我得到一个异常SocketTimeoutException。 我尝试过使用curl并上传同一个文件,效果很好 我尝试过不同的超时:20,30,50100秒,但在前10秒出现异常 源代码 MediaType OCTET_STREAM = MediaType.parse("application/octet-stream"); OkHttpClient client = new OkHttpClient(); client.s

我正试图从android上传一个文件,但我得到一个异常
SocketTimeoutException
。 我尝试过使用curl并上传同一个文件,效果很好

我尝试过不同的超时:20,30,50100秒,但在前10秒出现异常

源代码

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();
例外情况

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

看起来您还需要写入超时

client.setWriteTimeout(20, TimeUnit.SECONDS);

看起来您还需要写入超时

client.setWriteTimeout(20, TimeUnit.SECONDS);