0

I want to send a post request which contains jsonObject and an image and various other fields and I used MultiPart for image and have successfully uploaded the image but the jsonObject is uploaded in string format

Here is my Api Interface

 @Multipart
    @POST("operator/upload")
    Call<JsonObject>upload(@Header ("Authorization") String t,
                           @Part MultipartBody.Part img,
                           @Part("fullName") RequestBody name,
                           @Part("fatherName") RequestBody fathername,
                           @Part("aadharNumber") RequestBody aadharnumber,
                           @Part("dob") RequestBody dob,
                           @Part("registeredAddress") RequestBody address,
                           @Part("gender") RequestBody gender,
                           @Part("plantId") RequestBody plant,
                           @Part MultipartBody.Part data,
                           @Part("authorized") Boolean auth,
                           @Part("verified") Boolean ver) ;

Here is the Retrofit service function

public void userUpload(String p, MultipartBody.Part image, RequestBody name, RequestBody fathername, RequestBody aadharnumber, RequestBody dob, RequestBody address, RequestBody gender, RequestBody plant, MultipartBody.Part cap, Boolean aut, Boolean ver, Response_listner response_listner) {

        Call<JsonObject> call = api_interface.upload(p, image, name, fathername, aadharnumber, dob, address, gender, plant,cap,aut,ver);
        serveCall(call, response_listner);

    }

The ServeCall Method is here

private void serveCall(Call<JsonObject> call, Response_listner responce_listner) {

        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

                try {
                    responce_listner.onResponseSuccess(response);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

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

                try {
                    responce_listner.onResponseFailure(t);
                    Log.d("Network", t.getMessage());
                    Log.d("Network Cause", t.getCause().getMessage());
                    Log.d("NetworkStack", t.getStackTrace().toString());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

And My Main Code where I Made an Api Call

 HashMap<String, String> num2 = new HashMap<>();
                num2.put("client_id",String.valueOf(CLIENT_ID));
                num2.put("full_name",String.valueOf(FULL_NAME));
                num2.put("aadhaar_number",String.valueOf(AADHAR_NUMBER));
                num2.put("dob",String.valueOf(DOB));
                num2.put("gender",String.valueOf(GENDER));
                num2.put("face_status",String.valueOf(FACE_STATUS));
                num2.put("face_score",String.valueOf(FACE_SCORE));
                num2.put("zip",String.valueOf(ZIP));
                num2.put("profile_image",String.valueOf(PROFILE_IMAGE));
                num2.put("has_image",String.valueOf(HAS_IMAGE));
                num2.put("mobile_hash",String.valueOf(MOBILE_HASH));
                num2.put("raw_xml",String.valueOf(RAW_XML));
                num2.put("zip_data",String.valueOf(ZIP_DATA));
                num2.put("care_of",String.valueOf(CARE_OF));
                num2.put("share_code",String.valueOf(SHARE_CODE));
                num2.put("mobile_verified",String.valueOf(MOBILE_VERIFIED));
                num2.put("reference_id",String.valueOf(REFERENCE_ID));

MultipartBody.Part delta = MultipartBody.Part.createFormData("aadharDetails", num2.toString());

RetrofitClient.getInstance_new().userUpload(TokenData, image, name, fathername, aadharnumber, dobb, addresss, genderr, plant,delta, auth, ver, new Response_listner() {

                                    @Override
                                    public void onResponseSuccess(Response baseResponse) throws IOException, JSONException {
                                        if (baseResponse.isSuccessful()) {

                                            Log.e("Response",baseResponse.body().toString());

                                            JSONObject jsonObject = new JSONObject(baseResponse.body().toString());

   

                                                prog_close();
                                                Intent intent = new Intent(RegisterOP.this, Done.class);
                                                startActivity(intent);
                                                finish();
                                            }

//                                        }
                                        } else {
                                         
                                            Toast.makeText(RegisterOP.this, baseResponse.message(), Toast.LENGTH_SHORT).show();
                                        }
                                    }


                                    @Override
                                    public void onResponseFailure(Throwable throwable) {
                                        
                                        Toast.makeText(RegisterOP.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();


                                    }
                                });



So the data that I'm sending all is good except delta which is request body somehow it sends string rather than HashMap

Your Help Is Greatly Appreciated !!

Rahul Pandey
  • 520
  • 3
  • 15

0 Answers0