Andriod file download with server side (Client UI interface asynchronous request section) three

Source: Internet
Author: User

This article uses Asynctask to implement asynchronous requests with the following code:

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";p rivate progressdialog progressdialog=null;   Private URL Url=null;  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.download);  Get the user name passed in 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 ("hint");  Progressdialog.setmessage ("Please be patient, 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 (). Execute (URL);}});  } private class Downloadfilestask extends Asynctask<url, void, void> {@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);} }}

When registering, the username data is passed with intent.

Asynctask<url, Void, void>
There are three main parameters

    1. ParamsThe type of the parameters sent to the task upon execution. This article passes the URL data
    2. Progress, the type of the progress units published during the background computation.//progress bar
    3. ResultThe type of the result of the background computation. Results
This article does not adopt the progress bar form, only uses the ProgressDialog, therefore the second parameter is NULL, the third parameter chooses, this article in the Httputils package class already writes the file to the SD card, therefore also set to empty.

The overridden 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.  ui The main thread, initialize the parameters
  2. doinbackground (Params ...) , invoked on the background thread immediately after onpreexecute ()  finishes executing. This step was used to perform background computation the can take a long time. The parameters of the asynchronous task is passed to this step. The result of the computation must is returned by this step and would be passed the last step. This step can also Use publishprogress (Progress ...)  to publish one or more units of progress. These values is published on the UI thread, in Theonprogressupdate ( Progress ...)  step.   Non-main thread, time-consuming Operation
  3. onProgressUpdate(Progress...), invoked the UI thread after a call to publishProgress(Progress...) . The timing of the execution is undefined. This method was used to display any form of progress in the user interface and the background computation is still execut Ing. For instance, it can is 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. Results update

Andriod file download with server side (Client UI interface asynchronous request section) three

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.