1

I was trying to send string and images with a retrofit.

while I could get pass response with x-www-form-urlencoded & hashmap, but I need to send it with the image. so I use form-data, but I couldn't get the same response with the same name and value, tested it on postman and it goes passed the same as my x-www-form.

so here is the postman Postman request that got pass response

Method that doesn't goes through with form-data

@Multipart
@POST("report")
fun push(
    @HeaderMap headers: Map<String, String>,
    @Part("store") string: RequestBody
): Call<ReportingResponse>


RequestBody.create(MediaType.parse("multipart/form-data"), "testing") //#1 fail
RequestBody.create(MediaType.parse("text/plain"), "testing") //#2 fail

I tried both but couldn't get the same response as the postman, and what it looks is just like this Retrofit request interceptor on Android Studio

Method that goes Through with x-www-form

@FormUrlEncoded
@POST("report")
fun push(
    @HeaderMap headers: Map<String, String>,
    @FieldMap form: MutableMap<String, Any>
): Call<ReportingResponse>

What am I suppose to do?

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
hek
  • 59
  • 1
  • 8

1 Answers1

3

Step-1 : Create on interface method for call retrofit api

 @POST(Const.Task_Ans_FILE_NAME)
Call<TaskInfoBean>  verifyTaskAns(@Body RequestBody file);

Step-2: Use below code to send multipart image data along with other field in body.

RetroFitService retroFitService = RetrofitClient.getAppData();
    MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
    if (answer_type.equals("1")) {
        builder.addFormDataPart(Const.ANSWER, answer);
    } else {
        try {
            builder.addFormDataPart(Const.ANSWER, Const.SelectedFileName, RequestBody.create(MultipartBody.FORM, Const.BOS.toByteArray()));
        }catch (Exception e){
            Log.e(TAG, "doInBackground: "+e.getMessage() );
        }

    }
    builder.addFormDataPart(Const.LOGIN_ID, login_id)
            .addFormDataPart(Const.USER_ID, user_id)
            .addFormDataPart(Const.PLAY_ID, play_id)
            .addFormDataPart(Const.TASK_ID, task_id)
            .addFormDataPart(Const.SCENARIO, scenario)
            .addFormDataPart(Const.ANSWER_TYPE,answer_type)
            .addFormDataPart(Const.SKIP, skip);


    final RequestBody requestBody = builder.build();

    Call<TaskInfoBean> call = retroFitService.verifyTaskAns(requestBody);
    call.enqueue(new Callback<TaskInfoBean>() {
        @Override
        public void onResponse(Call<TaskInfoBean> call, Response<TaskInfoBean> response) {
            if(response.code()==200) {
                TaskInfoBean taskInfoBean = response.body();
                listener.OnVerifyTaskAns(taskInfoBean);
            }else{
                Log.e(TAG, "onResponse: "+response.toString() );
            }
        }

        @Override
        public void onFailure(Call<TaskInfoBean> call, Throwable t) {
            Log.e(TAG, "onFailure: " + t.toString());
        }
    });
    return null;
}

Step-3: Call this method in your activity/fragment.

Nimesh Patel
  • 1,394
  • 13
  • 26
  • 1
    Nice Answer!! It also solve the problem of File Object as in Android we usually get the URI or ByteArray. Just use this code directly to solve the issue – taranjeetsapra Oct 11 '21 at 12:23