Andriod file download includes server (client UI asynchronous request) 3
This document uses AsyncTask to Implement Asynchronous requests. The Code is as follows:
Public class downloadActivity extends Activity {private TextView myTextView = null; private Button button = null; private static final String path = "http: // 192.168.0.179: 8080/Myweb/download. do "; private ProgressDialog progreprogressdialog = null; private URL url = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. download); // get the passed user name Intent intent = getIntent (); String value = intent. getStringExtra ("username"); value = "hello" + "" + value; // myTextView = (TextView) findViewById (R. id. textView1); myTextView. setText (value); button = (Button) this. findViewById (R. id. download_btn); progressDialog = new ProgressDialog (this); progressDialog. setCancelable (false); progressDialog. setTitle ("prompt"); progressDialog. setMessage ("Please wait. The file is being downloaded .... "); try {url = new URL (path);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} button. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {// progressDialog. show (); new downloadfilestask(cmd.exe cute (url) ;}});} private class DownloadFilesTask extends AsyncTask
{@ Overrideprotected void onPreExecute () {progressDialog. show (); super. onPreExecute () ;}@ Overrideprotected Void doInBackground (URL... urls) {httpUtils. sendDownloadPost (urls [0]); return null ;}@ Overrideprotected void onPostExecute (Void result) {progressDialog. dismiss (); super. onPostExecute (result );}}}
During registration, username data is transmitted using Intent.
AsyncTask
There are three main parameters
Params
, The type of the parameters sent to the task upon execution. This article passes URL data
Progress
, The type of the progress units published during the background computation. // progress bar
Result
, The type of the result of the background computation. // results this document does not use progress bars and only uses ProgressDialog. Therefore, the second parameter is set to null. When the third parameter is selected, the httpUtils package class has been written to the SD card, therefore, it is also set to null.
Rewrite method:
onPreExecute()
, Invoked on the UI thread immediately after the task is executed. this step is normally used to setup the task, for instance by showing a progress bar in the user interface. initialize parameters in the main UI thread
doInBackground(Params...)
, Invoked on the background thread immediately afteronPreExecute()
Finishes executing. this step is used to perform background computation that can take a long time. the parameters of the asynchronous task are passed to this step. the result of the computation must be returned by this step and will be passed back to the last step. this step can also usepublishProgress(Progress...)
To publish one or more units of progress. These values are published on the UI thread, inonProgressUpdate(Progress...)
Step. Non-main thread, time-consuming operation
onProgressUpdate(Progress...)
, Invoked on the UI thread after a callpublishProgress(Progress...)
. The timing of the execution is undefined. this method is used to display any form of progress in the user interface while the background computation is still executing. for instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecute(Result)
, Invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. the result is updated.