亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android AsynTask處理返回數(shù)據(jù)和AsynTask使用get,post請求

 更新時間:2017年01月09日 11:02:12   作者:S丶black  
本文主要介紹了android AsynTask處理返回數(shù)據(jù)和AsynTask使用get,post請求方法。具有一定的參考價值,下面跟著小編一起來看下吧

Android是一個單線程模型,Android界面(UI)的繪制都只能在主線程中進(jìn)行,如果在主線程中進(jìn)行耗時的操作,就會影響UI的繪制和事件的響應(yīng)。所以在android規(guī)定,不可在主線中進(jìn)行耗時操作,否則將發(fā)生程序無響應(yīng)(ANR)問題。

解決辦法:開啟新的線程進(jìn)行耗時操作

開啟新的線程可以new Thread() 或?qū)崿F(xiàn)Runnable接口

什么要使用AsyncTask呢?

如果是使用Thread的run()方法,run()結(jié)束之后沒有返回值。所以必須要自己建立通信機(jī)制

AsyncTask將所有的線程通信都封裝成回調(diào)函數(shù),調(diào)用邏輯容易書寫。尤其是在異步處理結(jié)束之后,有回調(diào)函數(shù)進(jìn)行收尾處理??瓤?,程序員都懶的么

Android給我們提供的一個輕量級的用于處理異步任務(wù)的類:AsyncTask   當(dāng)然是那個簡單就用那個咯

最后還有一點就是:Android 4.0后禁止在UI線程中執(zhí)行網(wǎng)絡(luò)操作~不然會報:android.os.NetworkOnMainThreadException

什么是AsyncTask(原諒寶寶偷的圖   嘿嘿  不過真的解釋的很清楚呢)

注意:

 Task的實例必須在UI Thread中創(chuàng)建

 execute方法不惜在UI thread中創(chuàng)建

 task只能被執(zhí)行一次 多次調(diào)用時會出現(xiàn)異常

通用AsyncTask 以及在主線程中使用網(wǎng)絡(luò)請求回返的數(shù)據(jù)

   通用AsyncTask是什么意思呢 發(fā)送不同的請求返回不同類型的數(shù)據(jù) 難道要一個類型寫個AsyncTask 豈不是麻煩死咯

   還有一種情況  我們通過異步任務(wù)得到了一個對象然后在一下行立刻使用這個對象邏輯完全沒問題但是運(yùn)行之后會報空指針異常。這是怎么回事呢?       

   AsycnTask開始了一個新的線程,但是主線程并沒有停止還在繼續(xù)運(yùn)行,馬上就使用這個對象,而你新開的線程可能正在訪問網(wǎng)絡(luò)這個對象為空

   你無法確定AsycnTask什么時候才能獲取到數(shù)據(jù),網(wǎng)快嗖的一下就好了,網(wǎng)慢就要等好久。

看一個簡略的小例子

首先呢  我們使用異步任務(wù)的時候要處理不同類型的數(shù)據(jù)把這個Http設(shè)置泛型類第三個參數(shù)返回值類型設(shè)置為泛型不管你是什么類型的數(shù)據(jù)全部ok

我又寫了一個接口作為Http的屬性  在onPostExecute方法調(diào)用其中的onResponse方法在Test中實現(xiàn)接口

這個接口的作用完全可以理解為一個監(jiān)聽事件 checkbox的改變監(jiān)聽觸發(fā)條件是 是否選中這個接口監(jiān)聽是否有數(shù)據(jù)  完成網(wǎng)絡(luò)訪問有數(shù)據(jù)的時候就調(diào)用

我們在主線程中完成接口的實現(xiàn)已經(jīng)在主線程中實現(xiàn)了返回來的數(shù)據(jù)還不是任君宰割阿~~~~~

 public class Http<T> extends AsyncTask<String,Void,T> {
 private OnResponseListener<T> listener;
 public void setListener(OnResponseListener<T> listener) {
 this.listener = listener;
 }
 @Override
 protected T doInBackground(String... params) {
 return null;
 }
 @Override
 protected void onPostExecute(T t) {
 super.onPostExecute(t);
 if (listener!=null){
  listener.onResponse(t);
 }
 }
 //接口 類似一個監(jiān)聽事件
 public interface OnResponseListener<T>{
 void onResponse(T t);
 }
}
//獲取數(shù)據(jù)的測試類
public class Test {
 //要獲取的user對象
 private User user1=null;
 public void get(){
 //創(chuàng)建網(wǎng)絡(luò)訪問實例
 Http<User> http=new Http<User>();
 //重寫接口
 http.setListener(new Http.OnResponseListener<User>() {
  @Override
  public void onResponse(User user) {
  user1=user;
  }
 });
 http.execute("xxx.balabala.com");
 }
}

在發(fā)送請求的時候很容易就帶個參數(shù),請求的方式呢 無非就是get,post 兩者的區(qū)別呢大白話的說get不安全參數(shù)通過url直接傳過去post安全參數(shù)加密一下子

下面貼一下AsyncTask在get和post請求時核心代碼doInBackground方法

GET

protected T doInBackground(String... params) {
 //網(wǎng)絡(luò)連接對象
 HttpURLConnection connection=null;
 //輸入流 獲取網(wǎng)絡(luò)數(shù)據(jù)
 InputStream is=null;
 //字節(jié)數(shù)組輸出流
 ByteArrayOutputStream bos=null;
 try {
  //獲取網(wǎng)絡(luò)連接對象
  connection=(HttpURLConnection) new URL(params[0]).openConnection();
  //設(shè)置get請求 必須大寫
  connection.setRequestMethod("GET");
  //獲取網(wǎng)絡(luò)請求碼 200 400 500之類 不懂百度
  int code=connection.getResponseCode();
  if(code==200){
  //獲取流
  is=connection.getInputStream();
  //臨時字節(jié)數(shù)組
  byte [] b=new byte[1024];
  int len=-1;
  bos=new ByteArrayOutputStream();
  while ((len=is.read(b))!=-1){
   //寫入數(shù)據(jù)
   bos.write(b,0,len);
  }
  String json=bos.toString("utf-8");
  T t=JSON.parseObject(json,type);
  return t;
  }else{
  Log.e("error","網(wǎng)絡(luò)訪問失敗==========="+code);
  }
 } catch (IOException e) {
  e.printStackTrace();
 }finally {
  try {
  if (bos!=null){
   bos.close();
  }
  if (is!=null){
   is.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
  if (connection!=null){
  connection.disconnect();
  }
 }
 return null;
 }

POST

post和get的區(qū)別  就是post多了一段處理參數(shù)的代碼

protected T doInBackground(String... params) {
 //分割url 分為地址和參數(shù)兩部分
 String[] strArr=params[0].split("\\?");
 HttpURLConnection connection=null;
 //輸出流
 OutputStream os=null;
 //輸入流
 InputStream is=null;
 ByteArrayOutputStream bos=null;
 try {
  connection=(HttpURLConnection) new URL(strArr[0]).openConnection();
  connection.setRequestMethod("POST");
  //設(shè)置允許輸入 輸出 默認(rèn)值true 不寫也可以
  connection.setDoOutput(true);
  connection.setDoInput(true);
  os=connection.getOutputStream();
  //把參數(shù)寫入
  os.write(strArr[1].getBytes("utf-8"));
  os.close();
  int code=connection.getResponseCode();
  if(code==200){
  is=connection.getInputStream();
  byte [] b=new byte[1024];
  int len=-1;
  bos=new ByteArrayOutputStream();
  while ((len=is.read(b))!=-1){
   bos.write(b,0,len);
  }
  String json=bos.toString("utf-8");
  T t=JSON.parseObject(json,type);
  return t;
  }else{
  Log.e("error","網(wǎng)絡(luò)訪問失敗==========="+code);
  }
 } catch (IOException e) {
  e.printStackTrace();
 }finally {
  try {
  if (bos!=null){
   bos.close();
  }
  if (is!=null){
   is.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
  if (connection!=null){
  connection.disconnect();
  }
 }
 return null;
 }

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論