Use of AsyncTask and AsyncTask
1 --- network Permissions
2 -- the main thread calls the AsyncTask class instance call
/**
* 1 MAsyncTask is instantiated in the main thread;
* 2 doInBackground in the subthread, the data requested by the subthread will be sent to the method (onPostExecute) running in the main thread)
* 3 the underlying principle is Handler + Thread;
*/
New masynctask(cmd.exe cute (urlString );
3 --- compile the AsyncTask class;
/*** Create an asynchronous task class * The first generic --- is the passed interface address * The second parameter is the request interface progress * The third parameter is the data returned after the asynchronous task is processed */class MAsyncTask extends AsyncTask
{Public MAsyncTask () {super ();}/*** String... the strings parameter is a variable parameter. * This method runs in the sub-thread to request the network. */@ Override protected String doInBackground (String... strings) {String url = strings [0]; String netjson = NetUtils. getNetjson (url); // return the json from the network request; Log. e ("wzq", "doInBackground:" + netjson); return netjson;}/*** the parameter s received by this method is the value returned by doInBackground; * This method runs in the main thread and updates the ui. ** @ param s */@ Override protected void onPostExecute (String s) {super. onPostExecute (s); // This parameter s is the returned json TV. setText (s);}/*** network access is time-consuming. This method is used to update the ui progress; * It is also run in the main thread; this method will be called back multiple times; * @ param values */@ Override protected void onProgressUpdate (Void... values) {super. onProgressUpdate (values); // calculate the current value; Calculate a percentage; update the progress bar ui ;}}