Android : upload Image and JSON using MultiPartEntityBuilder

后端 未结 3 1677
说谎
说谎 2020-12-09 05:19

I try to upload data to server, my data containing multiple images and large JSON, before it, I Try to send with convert image to string using base64

3条回答
  •  自闭症患者
    2020-12-09 06:17

    To send binary data you need to use addBinaryBody method of MultipartEntityBuilder. Sample of attaching:

    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    //Image attaching
    MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
    File file;
    multipartEntity.addBinaryBody("someName", file, ContentType.create("image/jpeg"), file.getName());
    //Json string attaching
    String json;
    multipartEntity.addPart("someName", new StringBody(json, ContentType.TEXT_PLAIN));
    

    Then make request as usual:

    HttpPut put = new HttpPut("url");
    put.setEntity(multipartEntity.build());
    HttpResponse response = client.execute(put);
    int statusCode = response.getStatusLine().getStatusCode();
    

提交回复
热议问题