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
Params
The type of the parameters sent to the task upon execution. This article passes the 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 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:
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
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
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.
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