安卓 : upload Image and JSON using MultiPartEntityBuilder

标签 android json http-post multipartentity

我尝试将数据上传到服务器,我的数据包含多个图像和大型 JSON,在此之前,我尝试使用 base64 将图像转换为字符串发送并发送我以前用 JSON 转换过的另一个数据和图像,但我在这里遇到问题 OutOfMemory,所以我读了一个解决方案,说我必须尝试使用​​ MultipartEntityBuilder。 我仍然感到困惑,不明白如何使用 MultiPartEntityBuilder 来实现,有没有人可以帮助我使用 MultiPartEntityBuilder 来实现?这是我的代码:

    try{
    //membuat HttpClient
    //membuat HttpPost
    HttpPost httpPost= new HttpPost(url);
    SONObject jsonObjectDP= new JSONObject();
    System.out.println("file audio "+me.getModelDokumenPendukung().getAudio());
    jsonObjectDP.put("audio_dp",MethodEncode.EncodeAudio(me.getModelDokumenPendukung().getAudio()));
    jsonObjectDP.put("judul_audio",me.getModelDokumenPendukung().getJudul_audio());
    jsonObjectDP.put("ket_audio",me.getModelDokumenPendukung().getKet_audio());
    JSONArray ArrayFoto= new JSONArray();

    //This loop For my multiple File  Images
    List<ModelFoto>ListFoto=me.getModelDokumenPendukung().getListFoto();
    for (int i=0; i<ListFoto.size();i++) {
        JSONObject jsonObject= new JSONObject();
        jsonObject.put("foto", ListFoto.get(i).getFile_foto());
        jsonObject.put("judul_foto", ListFoto.get(i).getJudul_foto());
        jsonObject.put("ket_foto", ListFoto.get(i).getKet_foto());
        ArrayFoto.put(jsonObject);

    }

    JSONObject JSONESPAJ=null;
     JSONESPAJ = new JSONObject();
     JSONObject JSONFINAL = new JSONObject();
            JSONESPAJ.put("NO_PROPOSAL",me.getModelID().getProposal());
            JSONESPAJ.put("GADGET_SPAJ_KEY",me.getModelID().getIDSPAJ());
            JSONESPAJ.put("NO_VA",me.getModelID().getVa_number());
            JSONESPAJ.put("Dokumen_Pendukung",jsonObjectDP);

            JSONFINAL.put("ESPAJ", JSONESPAJ);
            JSONFINAL.put("CLIENT", "ANDROID");
            JSONFINAL.put("APP", "ESPAJ");

            MultipartEntityBuilder multiPartEntityBuilder= MultipartEntityBuilder.create();
    multiPartEntityBuilder.addPart("ESPAJ",JSONFINAL.toString());

    httpPost.setEntity(multiPartEntityBuilder.build());

    HttpResponse httpResponse = httpclient.execute(httpPost);


    inputStream = httpResponse.getEntity().getContent();
        if(inputStream != null)
        result = convertInputStreamToString(inputStream);
    else
        result = "Did not work!";
}catch(OutOfMemoryError e){
      Log.e("MEMORY EXCEPTION: ", e.toString());
} catch(ConnectTimeoutException e){
    Log.e("Timeout Exception: ", e.toString());
} catch(SocketTimeoutException ste){    
    Log.e("Timeout Exception: ", ste.toString());
} catch (Exception e) {
//    Log.d("InputStream", e.getLocalizedMessage());
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
//      hasil=line;
        result += line;

    inputStream.close();
    return result;

}   

有没有人可以帮我教教我如何使用 MultiPartEntityBuilder 发送 JSON 和 Image?

最佳答案

要发送二进制数据,您需要使用 MultipartEntityBuilderaddBinaryBody 方法。附加示例:

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

然后像往常一样发出请求:

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

关于安卓 : upload Image and JSON using MultiPartEntityBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24239923/

相关文章:

java - Android 中的堆转储和内存使用差异?

android - 如何在不使用按钮的情况下自动显示数据?

json - Angular Base64 文件上传为 JSON

java - 开始时带有按钮的布局

android - HttpPost 在 Android 3.2 上的 HttpClient 执行速度明显低于 2.3.3

java - 如何在android中选择和取消选择图像?

android - ADB 服务器没有 ACK

javascript - 使用 JSON 数组查找用户的最佳时间

android - 在android中通过http post方法发送json对象

php - 最近在插件中创建的 PHP 文件未运行 WordPress PHP 函数