我在尝试使用改型2.0.2和okhttp 3.2.0将图像上载到服务器时遇到IO异常

at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:199                                                                              at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)                                                                                  at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)                                                                          at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)                                                                              at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)                                                                                                                                                          at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)                                                                                                                                                          at okhttp3.RealCall.getResponse(RealCall.java:241)                                                                                                                                                          at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)                                                                                                                                                         at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)                                                                                                                                                          at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)                                                                                                                                                         at com.palaverplace.palaverplace.controller.api.RetrofitBase$1.intercept(RetrofitBase.java:74)                                                                                                                                                          at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)                                                                                                                                                          at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                                                                                                     at okhttp3.RealCall.access$100(RealCall.java:30)
                                                                                                                                                      at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
                                                                                                                                                      at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                                                                                      at java.lang.Thread.run(Thread.java:818)
                                                                                                                                                   Caused by: java.io.EOFException: \n not found: size=0 content=...
                                                                                                                                                      at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
                                                                                                                                                      at okhttp3.internal.http.Http1xStream.readResponse

我用过
@Multipart
    @PUT("/common/imageUpload")
    Call<Base> updateProfPic(@QueryMap HashMap<String,String> requestBody, @Part("image_file") RequestBody file);

在请求类中
 MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MEDIA_TYPE_PNG, file);


        HashMap<String,String> map = new HashMap<>();
        map.put("key1",value1);
        map.put("key2",value2);
        map.put("key3",value3);

        Call<Base> call = RetrofitBase.getRetrofitInstance(token).updatePic(map,requestBody);

可能是什么问题

最佳答案

感谢@bnk的评论,这有助于解决问题
只使用@Part("image_file") RequestBody file不会将图像上载到服务器
我把它改了个样子

 @Multipart
        @PUT(Const.IMAGE_UPLOAD)
        Call<Base> updatePic(@QueryMap HashMap<String,String> body, @Part
MultipartBody.Part file);

像这样调用接口
   RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),  new File(filePath));
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", new File(filePath).getName(), requestFile);


     Call<Base> call = RetrofitBase.getRetrofitInstance().updatePic(map,body);
     call.enqueue(new Callback<Base>() {
 @Override
            public void onResponse(Call<Base> call, Response<Base> response) {
                //Success response
            }

            @Override
            public void onFailure(Call<Base> call, Throwable t) {
                Log.e(LOG_TAG, "Failed to register device ", t);

            }

        });

希望它能帮助某人..
信用:BNK's Answer

关于android - 尝试在改造版2.0.2中将图像上传到服务器时出现EOF异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36810722/

10-09 01:42