2013-11-26 59 views
0

我正在開發一個應用程序,可以從設備庫,相機上傳多個圖像到服務器。 該功能在Galaxy S2和模擬器中正常工作,但是沒有在Galaxy S3上運行,S4 它在.execute停止,然後一段時間後它給了我 「recvfrom失敗:ECONNRESET(連接重置由對等體)某事正在寫入。 如果我只用文本執行功能,沒有圖像,它完美的作品。然而,當我附上圖像(小或大),它給了我例外。上傳圖片通過HttpClient,MultipartEntityBuilder Galaxy S3,S4

public static void executeMultipartPost(Context cont,String strImage1,String strImage2,String strImage3,String strImage4,String strData,String iCatgId,String strEmail,String strMobile,String strTokenId){ 
     try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost poster = new HttpPost("http://amazonkwt.com/cmsAmazon/WebService/frmNewClassifiedAd.aspx"); 
     File image1,image2,image3,image4; 

     if(!strImage1.equals("")) 
     image1 = new File(cont.getCacheDir() +"/"+strImage1); 

     if(!strImage2.equals("")) 
      image2 = new File(cont.getCacheDir() +"/"+strImage2); 

     if(!strImage3.equals("")) 
      image3 = new File(cont.getCacheDir() +"/"+strImage3); 

     if(!strImage4.equals("")) 
      image4 = new File(cont.getCacheDir() +"/"+strImage4); 
     Charset chars = Charset.forName("UTF-8"); 
     MultipartEntityBuilder entity = MultipartEntityBuilder.create();  
     entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
     entity.addPart("category_id", new StringBody(iCatgId)); 
     entity.addPart("text", new StringBody(strData,chars)); 
     entity.addPart("phone", new StringBody(strMobile)); 
     entity.addPart("lang", new StringBody("A")); 
     entity.addPart("Email", new StringBody(strEmail)); 
     entity.addPart("uID", new StringBody(strTokenId)); 
     if(image1!=null) 
     entity.addPart("img1", new FileBody(image1)); 
     if(image2!=null) 
     entity.addPart("img2", new FileBody(image2)); 
     if(image3!=null) 
     entity.addPart("img3", new FileBody(image3)); 
     if(image4!=null) 
     entity.addPart("img4", new FileBody(image4)); 
     poster.setEntity(entity.build()); 

     client.execute(poster, new ResponseHandler<Object>() { 
      public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { 
      HttpEntity respEntity = response.getEntity(); 
     String responseString = EntityUtils.toString(respEntity); 

      } }); 
     } catch (Exception e){ 
     e.getMessage(); 
     } 
    } 

}

+1

'例外'?? – njzk2

+0

打印出e.printStackTrace()併發布在這裏 – sgarman

回答

1

爲什麼不去做它作爲一個帖子?

   String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT); 

      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost("http://SERVER/API/"); 


      List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
      pairs.add(new BasicNameValuePair("image", image_str)); 
      pairs.add(new BasicNameValuePair("uID", ""+strTokenId)); 
      pairs.add(new BasicNameValuePair("phone",strMobile)); 

      try { 
        post.setEntity(new UrlEncodedFormEntity(pairs)); 
      } catch (UnsupportedEncodingException e) { 

      } 
      try { 
        HttpResponse response = client.execute(post); 


      } catch (ClientProtocolException e) { 
      }    
+0

我曾嘗試過,它給了我相同的結果 – user3038204

+0

你會得到什麼錯誤?> – TheGeekNess