上传图片throught HttpClient的,MultipartEntityBuilder银河S3,S4银河、上传图片、throught、MultipartEntityBuilder

2023-09-07 09:59:58 作者:Justin你是王

正在开发,从设备图库,相机服务器上传多张图片的应用程序。该功能可在银河S2和模拟器正常,但是是银河S3,S4犯规运行它停在.execute再经过一段时间它给了我recvfrom的失败:ECONNRESET(连接被对方​​复位)东西写。如果我只用文字和没有图像EXCUTE功能它完美。然而,当我附上图片(或大或小),它给我的异常。

Am developing an app that uploads multiple images from device Gallery,Camera to server. The Function works fine in Galaxy S2 and emulator, however is doesnt run on Galaxy S3,S4 it stops at .execute then after a while it gives me "recvfrom failed: ECONNRESET (Connection reset by peer) something is writing." If i excute the function with text only and no images it works perfectly. however when i attach images (small or large) it gives me the exception.

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();
      }
    }

}

推荐答案

为什么不去做一个帖子?

Why not do it as a post?

                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) {
            }