Andriod file download includes server (client UI asynchronous request) 3

Source: Internet
Author: User

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

 

 

  1. Params, The type of the parameters sent to the task upon execution. This article passes URL data
  2. Progress, The type of the progress units published during the background computation. // progress bar
  3. 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:

     

    1. 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
    2. 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
    3. 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.
    4. 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.

       

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.