user3038204
user3038204

Reputation: 1

Upload Images throught HttpClient,MultipartEntityBuilder Galaxy S3,S4

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

}

Upvotes: 0

Views: 1195

Answers (1)

TheGeekNess
TheGeekNess

Reputation: 1647

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

Upvotes: 1

Related Questions