0

I need to make a request which is in nested multipart data. I need to send image in multi part form data and other details should also be in multipart. My Json request is as follow:

{
  "emailId": "vision.jav@avenger.com",
  "phoneNumber": "7417385811",
  "profileImage": "image",
  "password": "12345678",
  "customerDetails": {
    "firstName": "vison",
    "lastName": "vision"
  },
  "addressDetails": {
    "city": "chicago",
    "province": "NY",
    "postalCode": "654987",
    "latitude": "28.52",
    "longitude": "77.54"
  },
  "userRole": {
    "role": "CUSTOMER"
  }
}
Kumar Raj
  • 124
  • 1
  • 10

2 Answers2

0

You have to create one POJO class like your json structure and set all values.

then

@Multipart
@POST("uploadData.php")
Call<ServerResponse> uploadFile(@PartMap Map<String, RequestBody> map,@Part MultipartBody.Part file);

then You have to use mapping option to send data to the server.

Map<String, RequestBody> map = new HashMap<>();
        map.put("PRODUCTID", RequestBody.create(MediaType.parse("text/plain"), ProductId));

MultipartBody.Part imageFile = null;
        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/Download/Salty.png");
            if file != null) {                
                RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),
                        file);
                imageFile = MultipartBody.Part.createFormData("ImageFile", file.getName(), requestFile);
            }
        }
        catch (Exception ex)
        {
            Log.d("UploadStatus", "Multipart Image as exception occurs");
        }


ApiService uploadImage = ApiClient.getClient().create(ApiService.class);
    Call<ServerResponse> fileUpload = uploadImage.uploadFile(map,imageFile);
    fileUpload.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                Toast.makeText(MainActivity.this, "Success " + response.message(), Toast.LENGTH_LONG).show();
                Toast.makeText(MainActivity.this, "Success " + response.body().toString(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                Log.d("TAG", "Error " + t.getMessage());
            }
    });
Raja
  • 2,775
  • 2
  • 19
  • 31
  • It gives me exception as java.lang.IllegalArgumentException: @Part annotation must supply a name or use MultipartBody.Part parameter type. (parameter #2). I have created my interface as "@Multipart" "@POST("user/signup")" Call apiSignup(@Part MultipartBody.Part profileImage,@Part ApiRequest apiRequest); – Kumar Raj Apr 23 '18 at 06:15
  • @KumarRaj, See my updated post, You should use like Mapping option.. its working fine for me.. – Raja Apr 23 '18 at 06:21
  • Dear @Raja , Its also working from my side. I have struck when I need to send my request in nested(customerDetails and addressDetails ). Please have a look at my Json request. – Kumar Raj Apr 23 '18 at 06:45
  • Thanks for you help. It works. Actually I was struck by nested json element. So not able to map Its key value. Finally I found the solution. Your answer works for simple multipart field. Thanks again for your help. – Kumar Raj May 11 '18 at 03:27
  • Can you please help me with this detailed question? It would be really appreciate: https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-after-nougat-using-retrofit – Priyanka Singh Jul 10 '20 at 12:31
0

For reference ,I am posting the answer. I don`t know how json object are being mapped at backend. Finally, I found the solution afer so many tries. Its working for me. For the above posted request , just use a dot(.) along with inner json object for making keys as Follows.

  1. For inserting data for firstName, Use "customerDetails.firstName" as key.
  2. similarly for last name use "customerDetails.lastName" as key .
Kumar Raj
  • 124
  • 1
  • 10
  • Can you please help me with this detailed question? It would be really appreciate: https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-after-nougat-using-retrofit – Priyanka Singh Jul 10 '20 at 12:30