4
votes

I am new to android retrofit and need to upload image file using multipart request format. I was able to upload successfully using iphone multipart request. However, had trouble with android retrofit. I use https and token bearer authorization.

@Multipart
@POST("/Api/ApiSales/UploadImages")
void uploadImage(@Part("File") TypedFile file,
                      Callback<Response> callback);

RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(appController.getInstance().getURL());
            builder.setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Authorization", appController.getInstance().getAuthTokenString());
                }
            });
            builder.setLogLevel(RestAdapter.LogLevel.FULL);

            RestAdapter restAdapter = builder.build();

            RetrofitService service = restAdapter.create(RetrofitService.class);
            service.uploadImage(new TypedFile("image/png",toFile), new Callback<retrofit.client.Response>() {
                @Override
                public void success(retrofit.client.Response response, retrofit.client.Response response2) {
                    Log.i(TAG, response.toString());
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e(TAG, error.toString());
                }
            });

I got following error:

retrofit.RetrofitError: Write error: ssl=0x5ef8ad40: I/O error during system call, Connection reset by peer      

I set up server for http request, still got error:

  retrofit.RetrofitError: sendto failed: ECONNRESET (Connection reset by peer)
1
Something is wrong with the SSL connection. See similar ssl error here code.google.com/p/android/issues/detail?id=65463Nikola Despotoski
I tried other get/post json requests, all worked except uploading file request, so maybe something else is wrong. I removed ssl and used regular http request, still got errorTony Lin
Hey, did you find a solution to this? I am facing the same problem. TIAAbhinav
it seems there was a problem on my server's router, this code worked on the other server.Tony Lin

1 Answers

0
votes

I think your interface should be like this.

 @Multipart
    @POST("/Api/ApiSales/UploadImages") 
    void uploadImage(@Header("Authorization") String user, 
    @Part("File") TypedFile file, Callback<Response> callback);

you should first get your real image path from onActivityResult

final String imagePath = getRealPathFromURI(imageUri);

and the getRealPathFromURI will be like this.

private String getRealPathFromURI(Uri contentUri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(this, contentUri, projection, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

and the intialize of your TypedFile

File photoFile = new File(imagePath);// image will be your real path
        String mimeType = getMimeType(imagePath);
        TypedFile photoTypedFile;
        if (mimeType != null) {
            photoTypedFile = new TypedFile(mimeType, photoFile);
        } else {
            photoTypedFile = new TypedFile("image/jpg", photoFile);
        }

and your intialize of your Restadapter will be the same except adding headers and your call will be like.

RetrofitService service = restAdapter.create(RetrofitService.class);
            service.uploadImage("yourAuthorization", photoTypedFil, new Callback<retrofit.client.Response>() {
                @Override
                public void success(retrofit.client.Response response, retrofit.client.Response response2) {
                    Log.i(TAG, response.toString());
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e(TAG, error.toString());
                }
            });