How can I use an Async Task to upload a file to the server?

后端 未结 2 1480
情歌与酒
情歌与酒 2020-12-17 03:01

Below is my code for uploading a file to the server. But I\'m getting network exceptions even after several tries and even after adding strict mode.

I\'m new to Andr

2条回答
  •  清酒与你
    2020-12-17 03:45

    mButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
    
    new UploadImageTask().execute(); // initialize asynchronous task
    
    }});
    
    //Now implement Asynchronous Task
    
    
    public class Get_User_Data extends AsyncTask {
    
                private final ProgressDialog dialog = new ProgressDialog(
                MyActivity.this);
    
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }
            @Override
            protected Void doInBackground(Void... params) {
    
                        uploadImage(); // inside the method paste your file uploading code
                return null;
            }
    
            protected void onPostExecute(Void result) {
    
                // Here if you wish to do future process for ex. move to another activity do here
    
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
    
            }
        }
    

    For more information refer this link http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/

提交回复
热议问题