OKHTTP3 抛出 ConnectionShutdownException

OKHTTP3 throws ConnectionShutdownException

我正在使用 okhttp3 将图像发送到服务器,这样:

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 在 headers 中发送,不要怪我,这是构建服务的方式)。

总是进入onFailure()方法,启动ConnectionShutdownException:

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)

我没有在调用中设置任何拦截器。

我该怎么做才能发送我的图片?

谢谢。

编辑:

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

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请求。更改为:

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

(数据必须在headers中发送,但是post需要addFormDataPart,所以我也添加了它们)但是例外是一样的。

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

(我没资格发表评论)