2014-03-27 24 views
0

我想上傳圖像到服務器。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對象和圖像使用多感謝您的幫助

回答

0

檢查一旦這個代碼iam使用這一個上傳圖像到服務器

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost(urls[0]); 
MultipartEntity multipartContent = new MultipartEntity(); 
for(int i=0;i<allimagespath.size();i++){ 
Bitmap bm = ShrinkBitmap(allimagespath.get(i), 140, 140); 
String format = allimagespath.get(i).substring((allimagespath.get(i).lastIndexOf(".")+1) , allimagespath.get(i).length()); 
Bitmap bit=Bitmap.createScaledBitmap(bm, 140, 140, true); 
ByteArrayOutputStream blob = new ByteArrayOutputStream(); 
if(format.equalsIgnoreCase("png")){ 
    bit.compress(CompressFormat.PNG, 100 , blob); 
}else{ 
    bit.compress(CompressFormat.JPEG, 100 , blob); 
} 
bitmapdata = blob.toByteArray(); 
ByteArrayBody thumbbmp = new ByteArrayBody(bitmapdata, "thumb."+format); 
FileBody bin2 = new FileBody(new File(allimagespath.get(i)));    
multipartContent.addPart("uploaded_file["+i+"]", bin2); 
multipartContent.addPart("uploaded_thumb["+i+"]", thumbbmp); 
} 
multipartContent.addPart("count", new StringBody(""+allimagespath.size())); 
postRequest.setEntity(multipartContent); 
HttpResponse response = httpClient.execute(postRequest); 
HttpEntity entity = response.getEntity(); 
is = entity.getContent();