9

I would like to do a PUT request with this JSON body (containing a picture) and with Retrofit. I'm using it under Android:

{
    "Request": {
        "data": {
            "Key": "keydata",
            "param": {
                "title": "Testingpostmultipartimageupload",
                "photo": **"IMAGE BYTE DATA"**
            }
        }
    }
}

Any clues?

JJD
  • 50,076
  • 60
  • 203
  • 339
Patrick
  • 425
  • 4
  • 15

3 Answers3

17

Ok, I found a solution using multipart, somethings like that:

@Multipart
@PUT("/users/{id}")
void modifyPic(
    @Header("auth_token") String token,
    @Path("id") int userid,
    @Part("request[data][param][title]") String title,
    @Part("request[data][param][Photo]") TypedFile avatar,
    Callback<User> cb
);
JJD
  • 50,076
  • 60
  • 203
  • 339
Patrick
  • 425
  • 4
  • 15
  • How do you fill the others fields of your object? (title for example) – Labe Jan 07 '15 at 16:39
  • 1
    you just have to add @Part("request[data][param][title]") String title. I'll edit my answer to set complete answer. – Patrick Jan 09 '15 at 00:25
  • Thank you for that! Does it have to be a @Part annotation for others fields than the file's one? – Labe Jan 16 '15 at 15:59
  • Yes, this solution needs to use @Part for each fields. – Patrick Jan 19 '15 at 00:47
  • @Patrick Can you point me to a solution for my attempt to [send an image with JSON data in one request](http://stackoverflow.com/questions/29680158/how-to-send-multipart-form-data-with-retrofit)? – JJD Apr 21 '15 at 10:21
  • @JJD It looks like you found a solution? – Patrick Apr 27 '15 at 23:31
0

You need to put image data in byte by using multipart form data.

try {
    HttpPost httppost = new HttpPost("some url");
    MultipartEntity multipartEntity = 
        new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    multipartEntity.addPart("Image", new FileBody(image));
    httppost.setEntity(multipartEntity);
    mHttpClient.execute(httppost, new YOURHANDLER());
} catch (Exception e) {
    Log.e(ServerCommunication.class.getName(), e.getLocalizedMessage(), e);
}

To send post request using parameters

HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

if (values != null) {
    for (Map.Entry<String, String> entry : values.entrySet()) {
        nameValuePairs.add(
            new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
}
JJD
  • 50,076
  • 60
  • 203
  • 339
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Thanks. How do you add to whole json object? By the way, i'm trying to do this using Retrofit lib. – Patrick Jun 11 '14 at 14:45
  • you cannot send json object until and unless you server accepting json string and parsing it into object. Usually post requests are send with key value pairs. see my updated answer – Murtaza Khursheed Hussain Jun 11 '14 at 14:46
  • @MurtazaHussain I need your help I have JSON string and I need to add selected PHOTO to upload it with it can. can I get you mail ? – Chlebta Nov 19 '14 at 07:08
0

Retrofit only takes multipart and requestbody for its multipart.

Call<SubmitLevel1Part2IconResp> loadLevel1halfIconswithImage(@Part("headerdata[relation][icon_type]") RequestBody icon_type, @Part("headerdata[relation][name]") RequestBody name, @Part MultipartBody.Part file);

And then in java

 // MultipartBody.Part is used to send also the actual filename
 MultipartBody.Part body =  MultipartBody.Part.createFormData("headerdata[relation][relative_image]", fileUpload.getName(), requestFile);



call = service.loadLevel1halfIconswithImage(icon_type, name, body);
andylee
  • 255
  • 2
  • 6