AMAN SINGH
AMAN SINGH

Reputation: 3561

Retrofit2 : How to upload image with JSONObject in android

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.

Upvotes: 1

Views: 1975

Answers (2)

Photon Point
Photon Point

Reputation: 808

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...   
    }   

Upvotes: 3

Vishal Patoliya ツ
Vishal Patoliya ツ

Reputation: 3238

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);

Upvotes: 0

Related Questions