1

I'm trying to post JSONObject with one image which is taken by camera run time. how to post an Image using retrofit in android. This is my Interface

 @Multipart
@POST("/upload")
Call<Response> getDetails(@Part("empsno") String  empsno,
                                @Part("time")String deliveryTime,
                                @Part("uploadFile") MultipartBody.Part part,
                                @Part("remarks")String remarks,
                                @Part("receiver")String receivedBy,
                                @Part("Address")String ipAddress
                                );

code i used to upload image with other details

 JSONObject oJSONObject = new JSONObject();
        oJSONObject.put("empsno", strEmpsno);
        oJSONObject.put("time", strtime);
        oJSONObject.put("remarks", strRemarks);
        oJSONObject.put("receiver", strReceiver);
        oJSONObject.put("Address", straddress);
        oJSONObject.put("uploadFile", imageFolderPath + "/" + imageName);

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile);

xInterface interface = retrofit.create(xInterface.class);
Call<Response> podResponsecall = interface.getDetails(strEmpsno, strtime,
                body, strRemarks, strReceiver, straddress);


  podResponsecall.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Response<Response> response) {
                String val = response.body() + "";
                Log.e(TAG, "onResponse: " + val);
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure: " + t.getLocalizedMessage());
            }
        });

Output - onFailure: JSON must start with an array or an object.

I don't know weather this is right or wrong. Please help me to post some images as well as other details using Retrofit2 beta 3.

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44

2 Answers2

3

Service.class :

    @Multipart
    @POST("upload")
    Call<Void> upload(@Part("model") RequestBody model,  @Part MultipartBody.Part file); 

Retrofit 2 part :

    Gson gson = new GsonBuilder().setLenient().create();
    RequestBody modelBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(modelPojo));
    RequestBody reqFileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part mPart1 = MultipartBody.Part.createFormData("file","name",reqFileBody);

Java Spring :

   @PostMapping(value="/upload" ,consumes = {"multipart/form-data","application/json"})
    public ResponseEntity<Object> uploadImagebyEmail(@RequestPart("model") Object model,@RequestPart("file") MultipartFile uploadfile  ) throws IOException {
    return //codes here...   
    }   
Photon Point
  • 798
  • 2
  • 13
  • 23
  • is it possible to send only one POJO containing Multipart Image and other Parameters like String? If it;s possible, how can i do it? – Avi Patel Nov 18 '18 at 18:47
  • You may dispatch images as a base64 string in an object otherwise i do not know any way. – Photon Point Nov 19 '18 at 08:13
0

Replace

@Multipart
@POST("/upload")
Call<ResponseBody> getDetails(@Part("empsno") RequestBody empsno,
                                @Part("time")RequestBody deliveryTime,
                                @Part("uploadFile") MultipartBody.Part part,
                                @Part("remarks")RequestBody remarks,
                                @Part("receiver")RequestBody receivedBy,
                                @Part("Address")RequestBody ipAddress
                                );

And also in

Call<ResponseBody> podResponsecall = interface.getDetails(strEmpsno, strtime,
                body, strRemarks, strReceiver, straddress);
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45