Android asynctask使用

來源:互聯網
上載者:User

標籤:

繼承asynctask,有三個參數

三個參數的含義是第一個表示輸入參數。第二個為progress,表示當前的進度,第三個為doInbackground
   返回值

須要一個參數傳入url,返回一個result結果,假設沒有網路則返回空

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 LinearLayout li;    private ListView  listview;    private myadapter2 myadapter;    private InputStream in;   private List<LinkedHashMap> data_set;   private httpget httpget;//須要一個參數傳入url,返回一個result結果,假設沒有網路則返回空    //三個參數的含義是第一個表示輸入參數。第二個為progress,第三個為doInbackground    //傳回值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為字串數組String geturl=new String();for(String url:urls)geturl+=url;Log.e("geturl", geturl);//httpget http=new httpget(geturl,in);//http.init();InputStream myin=httpget.get_IN();//讀取到檔案結束,會有問題//認證檔案讀取結束response=httpget.get_reponse(myin); return response;}protected void onPostExecute(String result){//doinBakcground 返回的結果//這裡面跟新uiif(result!=null){jsontest json=new jsontest(result);     json.init();    data_set=json.get_data();    myadapter.set_dataset(data_set);   // listview.setAdapter(myadapter);}}   }

在以下的代碼中更新

  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);  }


    

Android asynctask使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.