Search code examples
androidhttpmultifile-uploader

Issue for access the other http response during the upload progress in Background


I am uploading some file by using Apache library to upload multiple files on server by multipart entity builder. And it uploaded perfectly. But at the same time when I access the other http request during the uploading is going on then server is blocked my all other http request and once upload done then after giving me other http response. I do not understand what is wrong going? Please suggest me to resolve this issue.

Here I used code for upload:

@Override
            protected String doInBackground(String... urls) {
                String response1 = "";


                Prefs = context.getSharedPreferences(prefname, Context.MODE_PRIVATE);
                String memberid=Prefs.getString(ImageConstant.MEMBERID, "");

                try {

                       String url=ImageConstant.BASEURL+"image_fileupload.php";
                       Log.v(TAG, "url is: "+url);
                       HttpClient client = new DefaultHttpClient();
                        HttpPost post = new HttpPost(url);
                        MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
                        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);


                        for (int i = 1; i <= dataT.size(); i++) {
                            CustomGallery gallery=dataT.get(i-1);
                            String filename=gallery.sdcardPath;
                            Log.v(TAG, "file name: "+filename);

                            File file = new File(filename);
                            FileBody fb = new FileBody(file);
                            builder.addPart("file"+i, fb);



                        }



                        builder.addTextBody("member_id", memberid);
                        builder.addTextBody("count", String.valueOf(dataT.size()));

                        final HttpEntity yourEntity = builder.build();

                        CustomMultiPartEntity entity=new CustomMultiPartEntity(yourEntity, new ProgressListener() {

                            @Override
                            public void transferred(long num) {

                                publishProgress((int) ((num / (float) totalsizeimage) * 100));
                                //Log.v(TAG, "publish progress :"+totalsizeimage);
                                 ImageUtil.galleryLog(TAG, "publish progress :"+totalsizeimage);
                            }


                        });
                        totalsizeimage = entity.getContentLength();
                       // Log.v(TAG, "total size is: "+totalsize);
                        ImageUtil.galleryLog(TAG, "total size is: "+totalsize);
                        post.setEntity(entity);
                        HttpResponse response = client.execute(post);        
                        response1= getContent(response);

                    // response=postFile(dataT, memberid);

                    Log.v(TAG, "response is: "+ response1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
             return response1;
        }

Solution

  • Execute your asyntask using THREAD_POOL_EXECUTOR

    .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);