我尝试上传数据到服务器,我的数据包含多个图像和大JSON,在它之前,我尝试发送图像转换为字符串使用base64并发送我的另一个数据和图像我之前已经转换JSON,但我在这里面临问题OutOfMemory,所以我阅读其中一个解决方案,说我必须尝试使用MultipartEntityBuilder。 我仍然困惑,不明白如何做到这一点MultiPartEntityBuilder,有没有人可以帮助我做到这一点与MultiPartEntityBuilder?这是我的代码:Android:上传图片和JSON使用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
ListListFoto=me.getModelDokumenPendukung().getListFoto();
for (int i=0; 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和图像?
2014-06-16
Menma