We all know that multi-threaded applications are common in Android development, AsynTask you can achieve multi-threaded, it can perform a number of time-consuming tasks in the work thread.
When learning Android programming before encountered a problem when to implement requests for background data by AsynTask, you want to get to the data displayed in the UI thread. This premise is that there is a time-consuming task of executing the work must thread, the UI thread can be obtained these data, to fill the screen, otherwise it may appear time-consuming task is not executed, cause the UI thread corresponding data null.
The solution is to add an interface similar to the listener in succession AsynTask class.
The following write an example:
For example, front-end Android want to get information about all employees
First of all to write GetAllStuffRecordTask inherit AsynTask
1 public class GetAllStaffRecordTask extends AsyncTask<String,String ,List<StaffRecordBean>> { 2 private List<StaffRecordBean> list; 3 private OnResponseListener<List<StaffRecordBean>> listener; 4 5 public GetAllStaffRecordTask() { 6 } 7 public void setListener(OnResponseListener<List<StaffRecordBean>> listener) { 8 this.listener = listener; 9 } 10 @Override 11 protectedList <StaffRecordBean> the doInBackground (String strings ...) { 12 is 13 is // by the network access server login function 14 the URL URL = null ; 15 the try { 16 . 17 String urlStr = "HTTP: // ***** / Staff / findallstaffrecord " ; 18 is URL = new new the URL (urlStr); . 19 the HttpURLConnection Connection = (the HttpURLConnection) url.openConnection (); 20 is connection.setRequestProperty (" contentType "," UTF-. 8 "); // if the server to pass there are Chinese characters, characters prevent the garbage problem 21 InputStream IS =connection.getInputStream (); 22 is the InputStreamReader InputStreamReader = new new the InputStreamReader (IS); // convert stream 23 is the BufferedReader Reader = new new the BufferedReader (InputStreamReader); 24 String RES = reader.readLine (); 25 Log.e ( "RES" , RES ); 26 // parse JSON format string 27 Gson GSON = new new Gson (); 28 List = gson.fromJson (RES, new new TypeToken <List <StaffRecordBean >> () {} getType ());. 29 the Log .e ( "get the list",list.toString()); 30 } catch (MalformedURLException e) { 31 e.printStackTrace(); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 return list; 36 37 } 38 @Override 39 protected void onPostExecute(List<StaffRecordBean> lists ) { 40 super.onPostExecute(lists); 41 if (listener!=null){ 42 listener.onResponse (Lists); 43 is } 44 is } 45 // interface to a similar event listener 46 is public interface OnResponseListener <T> { 47 void onResponse (T T); 48 } 49 }
- Code Detailed: The main part is the addition of orange
- STEP 1: similar to a listener interface, i.e. 46 to 48 lines of code
- Step Two: OnResponseListener disposed in a class <T> listener object type (code line 3) named listener, where T is set to List <StaffRecordBean>
- The third step: set the listener is provided a method (Code 7-9) line
- A fourth step, to achieve onPostExcute class () method will need to return a list of lists be set to the return data (code lines 39 to 44)
The method calls for the UI thread:
Task GetAllStuffRecordTask = new new GetAllStuffRecordTask (); task.setListener ( new new GetAllStaffRecordTask.OnResponseListener <List <StaffRecordBean >> () { @Override public void onResponse (List <StaffRecordBean> List) { // get the required data here may be made the operation corresponding } }); task.execute ();
This OK
Finally, welcome criticism