3

I am getting an IO Exception while trying to upload image to server using retrofit 2.0.2 and okhttp 3.2.0

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

I used

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

And in the request class

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

What may be the issue

George Thomas
  • 4,566
  • 5
  • 30
  • 65
  • Please read http://stackoverflow.com/questions/36491096/retrofit-multipart-request-required-multipartfile-parameter-file-is-not-pre/ to see if it can help – BNK Apr 23 '16 at 22:44

1 Answers1

2

Thank You @BNK for your Comment, that helped to solve the issue

Just using @Part("image_file") RequestBody file wont upload the image to the server

I remodeled it like following to make it work

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

And call the interface like

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

            }

        });

Hope it helps someBody..

CREDIT: BNK's Answer

Community
  • 1
  • 1
George Thomas
  • 4,566
  • 5
  • 30
  • 65