HttpUrlConnection multipart file upload with progressBar

后端 未结 2 1891
春和景丽
春和景丽 2021-01-02 11:32

I want to check progress of uploading file by HttpUrlConnection. How I can do this? I\'ve tried to calculate bytes when writing data in OutputStream

2条回答
  •  自闭症患者
    2021-01-02 11:48

    Right this code in your activity...

    public class PublishPostToServer extends AsyncTask implements ProgressListenerForPost {

        public Context pContext;
        public long totalSize;
        private String response;
    
        public PublishPostToServer(Context context) {
            pContext = context;
    
        }
    
        protected void onPreExecute() {
            showProgressDialog();
        }
    
        @Override
        protected Boolean doInBackground(Void... params) {
            boolean success = true;
            try {
                response = NetworkAdaptor.getInstance()
                        .upLoadMultipartImageToServer(
                                "",
                                "",
                                "", this, this); // Add file path, Authkey, caption 
    
            } catch (Exception e) {
                success = false;
            }
            return success;
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            //validateResponse(result, response);
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
    
            try {
                if (mProgressDialog != null) {
                    mProgressDialog.setProgress(values[0]);
                }
            } catch (Exception exception) {
    
            }
        }
    
        @Override
        public void transferred(long num) {
            publishProgress((int) ((num / (float) totalSize) * 100));
        }
    
    }
    
    private void showProgressDialog() {
    
        try {
            String dialogMsg = "Uploading Image...";
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage(dialogMsg);
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setMax(100);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
        } catch (Exception exception) {
    
        }
    }
    

    Now, Make a NetworkAdapter Class

    public String upLoadMultipartImageToServer(String sourceFileUri, String auth_key, String caption, ProgressListenerForPost listiner, PublishPostToServer asyncListiner) { String upLoadServerUri = "" + "upload_image";

        HttpPost httppost = new HttpPost(upLoadServerUri);
    
        File file = new File(sourceFileUri);
    
        if (file.exists()) {
    
            FileBody filebodyVideo = new FileBody(file);
            CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE, listiner);
            try {
                multipartEntity.addPart("auth_key", new StringBody(auth_key));
                multipartEntity.addPart("caption", new StringBody(caption));
                multipartEntity.addPart("image", filebodyVideo);
                asyncListiner.totalSize = multipartEntity.getContentLength();
                httppost.setEntity(multipartEntity);
    
            }
    
            catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            DefaultHttpClient mHttpClient = new DefaultHttpClient();
            String response = "";
            try {
                response = mHttpClient.execute(httppost,
                        new MovieUploadResponseHandler());
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return response;
        } else {
            return null;
        }
    
    } 
    
    @SuppressWarnings("rawtypes")
    private class MovieUploadResponseHandler implements ResponseHandler {
    
        @Override
        public Object handleResponse(HttpResponse response)
                throws ClientProtocolException, IOException {
    
            HttpEntity r_entity = response.getEntity();
            String responseString = EntityUtils.toString(r_entity);
            // DebugHelper.printData("UPLOAD", responseString);
    
            return responseString;
        }
    
    }
    
    public static boolean isValidResponse(String resultData) {
        try {
    
        } catch (Exception exception) {
            //DebugHelper.printException(exception);
        }
        return true;
    }
    
    public String upLoadVideoToServer(String currentFilePath, String string,
            PublishPostToServer publishPostToServer,
            PublishPostToServer publishPostToServer2) {
        // TODO Auto-generated method stub
        return null;
    }
    

提交回复
热议问题