0

My requirement is to pass the request in the below format,

enter image description here

I don't have any idea on how to send request in the below format using a @PartMap,

{
"image", img1.jpg,
"image", img2.jpg,
"service_request", "asd123123asfsf"
}

The type for above format,

{
"<String>", <Image file>,
"<String>", <Image file>,
"<String>", "<String>"
}

My current method

private void uploadMultipleFiles() {

        SharedPreferences preferences = getApplicationContext().getSharedPreferences("MY_APP", Context.MODE_PRIVATE);
        String retrivedToken = "Token " + preferences.getString("TOKEN", null);
        String attachment_one = preferences.getString("Attachement_one", null);
        String attachement_two = preferences.getString("Attachement_two", null);

        File file_one = new File(attachment_one);
        File file_two = new File(attachement_two);
        
        RequestBody requestBody1 = RequestBody.create(MediaType.parse("*/*"), file_one);
        RequestBody requestBody2 = RequestBody.create(MediaType.parse("*/*"), file_two);

        MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("file1", file_one.getName(), requestBody1);
        MultipartBody.Part fileToUpload2 = MultipartBody.Part.createFormData("file2", file_two.getName(), requestBody2);

        Call<ResponseBody> call = ServiceBuilder.getInstance().getApi().uploadImages(retrivedToken, "multipart/form-data", fileToUpload1, fileToUpload2, filename);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (!response.isSuccessful()) {
                    System.out.println("Response Code: " + response.code());
                    return;
                } else {
                    System.out.println("Response Code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                System.out.println("Message: " + t.getMessage());
            }
        });
    }

Service method

@Multipart
    @POST("http://192.168.1.100:8000/yrtp/service/request/upload")
    Call <ResponseBody> uploadImages(@Header ("Authorization") String token, @Header("Content-Type") String type, @Part MultipartBody.Part file1, @Part MultipartBody.Part file2, @Part("service_request") RequestBody description);

I'm new in using the @PartMap in retrofit and android. Kindly help me with a solution. Million thanks in advance!

  • What is the issue? – Mr_vmh Jul 30 '20 at 11:29
  • I have no idea how to pass data in this format { "image", img1.jpg, "image", img2.jpg, "service_request", "asd123123asfsf" } using a @PartMap. – Sapthagiri Manivannan Jul 30 '20 at 11:42
  • @Swami I've edited my question for better understanding. Kindly check :) – Sapthagiri Manivannan Jul 30 '20 at 11:49
  • Make use of this link (https://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image) – Mr_vmh Jul 30 '20 at 11:50
  • Are you looking for json object formate? – Mr_vmh Jul 30 '20 at 12:02
  • @Mr_vmh I know to pass in Json format but my problem now is in the same request, I need to pass Same key and different values for images and additionally a key in string type and the value also in string format. Kindly refer the request model above. I'm also trying to find a solution for this. Meanwhile i would be happy if someone help me with this – Sapthagiri Manivannan Jul 30 '20 at 13:55

0 Answers0