尝试使用改装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);
可能是什么问题
答案 0 :(得分:2)
感谢@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