关于android:OKHTTP3抛出ConnectionShutdownException

OKHTTP3 throws ConnectionShutdownException

我正在使用okhttp3通过这种方式将图像发送到服务器:

1
2
3
4
5
6
7
8
9
10
final OkHttpClient cliente=new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .build();

(是的,params是在标头中发送的,请不要怪我,它是构建服务的方式)。

它总是以onFailure()方法输入,启动ConnectionShutdownException:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
okhttp3.internal.http2.ConnectionShutdownException
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:248)
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:231)
at okhttp3.internal.http2.Http2Codec.writeRequestHeaders(Http2Codec.java:117)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:50)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

我没有在通话中设置任何拦截器。

我该怎么做发送图片?

谢谢。

编辑:

根据克里斯托弗的建议,我这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final OkHttpClient cliente=new OkHttpClient();

    OkHttpClient client1 = cliente.newBuilder()
            .readTimeout(10000, TimeUnit.MILLISECONDS)
            .build();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .build();
    client1.newCall(request).enqueue(new Callback() {
       //stuff
     }

但是没有结果,同样的例外。

编辑2:

海猫回答后,我意识到这应该是一个POST请求。 变成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
final OkHttpClient cliente=new OkHttpClient();

    HttpUrl.Builder urlBuilder = HttpUrl.parse(getString(R.string.sendimageurl)).newBuilder();

    String url = urlBuilder.build().toString();
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("imagebase64", toBASE64(bitmap))
            .addFormDataPart("token", almacen.getToken())
            .build();

    Request request=new Request.Builder()
            .addHeader("imagebase64", toBASE64(bitmap))
            .addHeader("token", almacen.getToken())
            .url(url)
            .post(requestBody)
            .build();

(数据必须在标题中发送,但帖子需要addFormDataPart,因此我也将其添加到其中),但例外是相同的。


您的请求的类型为get()。 那是你要的吗?

(我缺乏评论的声誉)