Android Asynctask Use

Source: Internet
Author: User

Inherit Asynctask, there are three parameters

The meaning of the three parameters is the first one to represent the input parameter, the second is progress, the current progress, and the third is Doinbackground
return value

A parameter is required to pass in the URL, which returns a result if no network is returned empty

Import Java.io.inputstream;import java.util.arraylist;import Java.util.linkedhashmap;import Java.util.List;import Mybook.bookinfo;import mybook.httpget;import mybook.jsontest;import Android.os.asynctask;import android.util.Log; Import Android.widget.linearlayout;import Android.widget.listview;import Hdu.m12306.zzy.*;import Hdu.m12306.zzy.listview.myadapter2;public class MyTask extends Asynctask<string, void,string > {private LinearLa    Yout Li;    Private ListView ListView;    Private Myadapter2 Myadapter;   Private InputStream in;   Private list<linkedhashmap> Data_set;    Private HttpGet httpget;//need a parameter to pass in the URL, return a result results, if there is no network return NULL//three parameters meaning is the first to represent the input parameters, the second is progress, the third is a doinbackground return value public mytask (ListView listview,myadapter2 adapter,httpget httpget) {this.listview=listview;this.myadapter= Adapter;//this.in=in;this.httpget=httpget;new arraylist<linkedhashmap> ();} @Overrideprotected String Doinbackground (string...urls) {//TODO auto-generated method StubstriNg Response=null;//urls is a string array, string Geturl=new string (), and for (string url:urls) Geturl+=url; LOG.E ("Geturl", Geturl);//httpget http=new HttpGet (geturl,in);//http.init (); InputStream myin=httpget.get_in ();// Read to the end of the file, there will be a problem//certificate file read end Response=httpget.get_reponse (Myin); return response;} protected void OnPostExecute (String result) {//doinbakcground Returns the result//This inside with the new Uiif (result!=null) {jsontest json=new     Jsontest (result);    Json.init ();    Data_set=json.get_data ();   Myadapter.set_dataset (Data_set);   Listview.setadapter (Myadapter);}} }

Update in the following code

  public void On_fresh (final View v) {  mytask task=new mytask (listview,myadapter,http);  LOG.E ("Onfresh", url);    String []params=new String[]{url};  Task.execute (params);  }



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.