通过HttpClient,MultipartEntityBuilder Galaxy S3,S4上传图像

时间:2013-11-26 21:15:21

标签: android

正在开发一个应用程序,可以从设备库,相机到服务器上传多个图像。 该功能在Galaxy S2和模拟器中运行良好,但不能在Galaxy S3,S4上运行 它停在.execute然后一段时间后它给了我 “recvfrom失败:ECONNRESET(由同行重置连接)正在编写。” 如果我只使用文本执行该功能而没有图像,它可以完美地工作。 但是,当我附加图像(小或大)时,它给了我例外。

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 个答案:

答案 0 :(得分: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) {
            }