1

I have a fragment where, I click on Browse Button and open file manager and select the file and send it to server via POST Retrofit2.

I get the success message 200. The file is listed in server but it wont open. The size is 1kb. So, I think the file is not properly uploaded.

Following is my code.

Where am I going wrong?

            File origFile = new File(PathHolder);
            String getDirPath = origFile.getParent();
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), getDirPath);
            multipartBody = MultipartBody.Part.createFormData("uploadFiles",origFile.getName(),requestFile);

            new UploadFileAsyncTask().execute();

And the async task is

protected notificationVO doInBackground(Void... params) {
                    notificationVO res;
                    WebserviceImpl webservices = new WebserviceImpl();
                    res = webservices.notifyAttachment(token,multipartBody, getContext());

                    Log.e("File","browse uploaded");
                    return res;
        }

Api

 @Multipart
    @POST("upload")
    public Call<notificationVO>notifyAttachment(@Query("token")String token,
                                                @Part MultipartBody.Part attachFile);   // @Part MultipartBody.Part file

Implementation

 public notificationVO notifyAttachment(String token,MultipartBody.Part fileUri,final Context context){

        WebservicesApi mRestAPIWService = ApiUtilsForWS.getAPIService(context,);


        Call<notificationVO> call = mRestAPIWService.notifyAttachment(token,fileUri);
        try {

            Response<notificationVO> response = call.execute();
            if(response.isSuccessful())
            {
                Log.e(TAG,"Success."+response.code());
                return response.body();

            }
            else
            {

                Log.e(TAG,"Failed."+response.code());
                return null;
            }

        } catch (IOException e1) {
            e1.printStackTrace();
            return null;
        }

    }
Shachi
  • 1,858
  • 3
  • 23
  • 41
  • Check from postman(postman rest client) to upload a file using same API – MathankumarK Jan 19 '18 at 13:23
  • Perfectly uploading from postman.. The problem is in Android code. – Shachi Jan 19 '18 at 13:32
  • I will post an answer for this before that please check the size of file that you are uploading. – MathankumarK Jan 19 '18 at 13:55
  • around 180 kb.... it goes perfectly well from postman.. and even downloads from server.... the issue can be of the RequestBody... where charset utf is added.... Content Type: Media Type : multipart/form-data; charset=utf-8 I dont know if this is the issue or not, but here is the link. https://stackoverflow.com/questions/46045952/what-does-utf-8-charset-change-in-a-multipart-form-data-http-request – Shachi Jan 19 '18 at 14:05

1 Answers1

1

Use interface like this below, remove @Query annotation add as @Part in your interface.

interface Service {
            @Multipart
            @POST("upload")
            Call<notificationVO> postImage(@Part MultipartBody.Part image, @Part("token") RequestBody name);
        }

Check your File path is valid and also check the file size to confirm that you are getting file properly from your file picker.

File origFile = new File(PathHolder);
int file_size = Integer.parseInt(String.valueOf(origFile .length()/1024));

If everything is OK then try this below option to upload your file it will work

RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), mFile);
        RequestBody token = RequestBody.create(MediaType.parse("text/plain"), file.getName());
// Service is interface name you can use your own interface name
        Service uploadImage = retrofit.create(Service.class);
        Call<notificationVO> fileUpload = uploadImage.postImage(fileToUpload, token);
        fileUpload.enqueue(new Callback<notificationVO>() {
            @Override
            public void onResponse(Call<notificationVO> call, Response<notificationVO> response) {

            }

            @Override
            public void onFailure(Call<notificationVO> call, Throwable t) {

            }
        });
MathankumarK
  • 2,717
  • 1
  • 17
  • 34