我想上传图像到服务器。android多部分图像上传与json对象
下面是代码,
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.yigit);
Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
MultipartEntity reqEntity = new MultipartEntity();
StringBody jsonBody = new StringBody(getNewDemandRequestParams(), "application/json",null);
FormBodyPart jsonBodyPart = new FormBodyPart("data", jsonBody);
reqEntity.addPart(jsonBodyPart);
if (getMainActivity().getImagesSavedData(0).size() > 0) {
for (int i = 0; i < getMainActivity().getImagesSavedData(0).size(); i++) {
File _file = new File(getMainActivity().getImagesSavedData(0).get(i).getFilePath());
FileBody _fileBody = new FileBody(_file, "image/jpg", "UTF-8");
FormBodyPart fileBodyPart = new FormBodyPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg", ""), _fileBody);
reqEntity.addPart(fileBodyPart);
reqEntity.addPart(getMainActivity().getImagesSavedData(0).get(i).getImageName().replace(".jpg",""), _fileBody);
}
}
post.setEntity(reqEntity);
String result = EntityUtils.toString(reqEntity);
Log.e("rsul", result);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
}
但问题是jsonBodyPart是包括斜杠。
请求正文是这样的:
{ “数据”=> “{\” 动作\ “:\” YENITALEP \ “\ ”应用\“:{\ ”版本\“:\” verisyon \ “},\” 数据\ “:{\” 发票\ “:[{\” imageName \ “:\” 1395914025134 \”,\ “注\”:\ “\” \ “类型\”:\ “FATURA \”,\ “typeNo \”:\ “0 \”}],\ “注\”:\ “\” \ “通知\”:[{\ “类型\”?:\“BeniArayñ\ “,”typeNo“:\”0 \“}]},\”device \“:{\”hardwareModel \“:\”m7 \“,\”model \“:\”HTC One \ \ “systemVersion \”:\ “4.4.2 \”,\ “UID \”:\ “00000000-7f39-faab-b500-7f280e9b4fed \”},\ “时间戳\”:\“日期(1391073711000 + 0200)\ “}”, “1395914025134”=>#, @ original_filename =“1395914025134.jpg”,@ content_type =“image/jpg; charset = UTF-8”,@hea ders =“Content-Disposition:form-data; name = \“1395914025134 \”; filename = \“1395914025134.jpg \”\ r \ nContent-Type:image/jpg; 的charset = UTF-8 \ r \ nContent传输编码:二进制\ r \ n“>}
我如何可以发布一个复杂的JSON对象和图像使用多感谢您的帮助