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

Android使用http協(xié)議與服務(wù)器通信的實例

 更新時間:2016年12月29日 08:55:08   作者:handspeaker  
本篇文章主要介紹了Android使用http協(xié)議與服務(wù)器通信,Android與服務(wù)器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post兩種方式。感興趣的小伙伴們可以參考一下。

網(wǎng)上介紹Android上http通信的文章很多,不過大部分只給出了實現(xiàn)代碼的片段,一些注意事項和如何設(shè)計一個合理的類用來處理所有的http請求以及返回結(jié)果,一般都不會提及。因此,自己對此做了些總結(jié),給出了我的一個解決方案。

首先,需要明確一下http通信流程,Android目前提供兩種http通信方式,HttpURLConnection和HttpClient,HttpURLConnection多用于發(fā)送或接收流式數(shù)據(jù),因此比較適合上傳/下載文件,HttpClient相對來講更大更全能,但是速度相對也要慢一點。在此只介紹HttpClient的通信流程:

1.創(chuàng)建HttpClient對象,改對象可以用來多次發(fā)送不同的http請求

2.創(chuàng)建HttpPost或HttpGet對象,設(shè)置參數(shù),每發(fā)送一次http請求,都需要這樣一個對象

3.利用HttpClient的execute方法發(fā)送請求并等待結(jié)果,該方法會一直阻塞當前線程,直到返回結(jié)果或拋出異常。

4.針對結(jié)果和異常做相應(yīng)處理 

根據(jù)上述流程,發(fā)現(xiàn)在設(shè)計類的時候,有幾點需要考慮到:

1.HttpClient對象可以重復(fù)使用,因此可以作為類的靜態(tài)變量

2.HttpPost/HttpGet對象一般無法重復(fù)使用(如果你每次請求的參數(shù)都差不多,也可以重復(fù)使用),因此可以創(chuàng)建一個方法用來初始化,同時設(shè)置一些需要上傳到服務(wù)器的資源

3.目前Android不再支持在UI線程中發(fā)起Http請求,實際上也不該這么做,因為這樣會阻塞UI線程。因此還需要一個子線程,用來發(fā)起Http請求,即執(zhí)行execute方法

4.不同的請求對應(yīng)不同的返回結(jié)果,對于如何處理返回結(jié)果(一般來說都是解析json&更新UI),需要有一定的自由度。

5.最簡單的方法是,每次需要發(fā)送http請求時,開一個子線程用于發(fā)送請求,子線程中接收到結(jié)果或拋出異常時,根據(jù)情況給UI線程發(fā)送message,最后在UI線程的handler的handleMessage方法中做結(jié)果解析和UI更新。這么寫雖然簡單,但是UI線程和Http請求的耦合度很高,而且代碼比較散亂、丑陋。

基于上述幾點原因,我設(shè)計了一個PostRequest類,用于滿足我的http通信需求。我只用到了Post請求,如果你需要Get請求,也可以改寫成GetRequest

package com.handspeaker.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.util.Log;

/**
 * 
 * 用于封裝&簡化http通信
 * 
 */
public class PostRequest implements Runnable {
  
  private static final int NO_SERVER_ERROR=1000;
  //服務(wù)器地址
  public static final String URL = "fill your own url";
  //一些請求類型
  public final static String ADD = "/add";
  public final static String UPDATE = "/update";
  public final static String PING = "/ping";
  //一些參數(shù)
  private static int connectionTimeout = 60000;
  private static int socketTimeout = 60000;
  //類靜態(tài)變量
  private static HttpClient httpClient=new DefaultHttpClient();
  private static ExecutorService executorService=Executors.newCachedThreadPool();
  private static Handler handler = new Handler();
  //變量
  private String strResult;
  private HttpPost httpPost;
  private HttpResponse httpResponse;
  private OnReceiveDataListener onReceiveDataListener;
  private int statusCode;

  /**
   * 構(gòu)造函數(shù),初始化一些可以重復(fù)使用的變量
   */
  public PostRequest() {
    strResult = null;
    httpResponse = null;
    httpPost = new HttpPost();
  }
  
  /**
   * 注冊接收數(shù)據(jù)監(jiān)聽器
   * @param listener
   */
  public void setOnReceiveDataListener(OnReceiveDataListener listener) {
    onReceiveDataListener = listener;
  }

  /**
   * 根據(jù)不同的請求類型來初始化httppost
   * 
   * @param requestType
   *      請求類型
   * @param nameValuePairs
   *      需要傳遞的參數(shù)
   */
  public void iniRequest(String requestType, JSONObject jsonObject) {
    httpPost.addHeader("Content-Type", "text/json");
    httpPost.addHeader("charset", "UTF-8");

    httpPost.addHeader("Cache-Control", "no-cache");
    HttpParams httpParameters = httpPost.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters,
        connectionTimeout);
    HttpConnectionParams.setSoTimeout(httpParameters, socketTimeout);
    httpPost.setParams(httpParameters);
    try {
      httpPost.setURI(new URI(URL + requestType));
      httpPost.setEntity(new StringEntity(jsonObject.toString(),
          HTTP.UTF_8));
    } catch (URISyntaxException e1) {
      e1.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
  }

  /**
   * 新開一個線程發(fā)送http請求
   */
  public void execute() {
    executorService.execute(this);
  }

  /**
   * 檢測網(wǎng)絡(luò)狀況
   * 
   * @return true is available else false
   */
  public static boolean checkNetState(Activity activity) {
    ConnectivityManager connManager = (ConnectivityManager) activity
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connManager.getActiveNetworkInfo() != null) {
      return connManager.getActiveNetworkInfo().isAvailable();
    }
    return false;
  }

  /**
   * 發(fā)送http請求的具體執(zhí)行代碼
   */
  @Override
  public void run() {
    httpResponse = null;
    try {
      httpResponse = httpClient.execute(httpPost);
      strResult = EntityUtils.toString(httpResponse.getEntity());
    } catch (ClientProtocolException e1) {
      strResult = null;
      e1.printStackTrace();
    } catch (IOException e1) {
      strResult = null;
      e1.printStackTrace();
    } finally {
      if (httpResponse != null) {
        statusCode = httpResponse.getStatusLine().getStatusCode();
      }
      else
      {
        statusCode=NO_SERVER_ERROR;
      }
      if(onReceiveDataListener!=null)
      {
        //將注冊的監(jiān)聽器的onReceiveData方法加入到消息隊列中去執(zhí)行
        handler.post(new Runnable() {
          @Override
          public void run() {
            onReceiveDataListener.onReceiveData(strResult, statusCode);
          }
        });
      }
    }
  }

  /**
   * 用于接收并處理http請求結(jié)果的監(jiān)聽器
   *
   */
  public interface OnReceiveDataListener {
    /**
     * the callback function for receiving the result data
     * from post request, and further processing will be done here
     * @param strResult the result in string style.
     * @param StatusCode the status of the post
     */
    public abstract void onReceiveData(String strResult,int StatusCode);
  }

}

代碼使用了觀察者模式,任何需要接收http請求結(jié)果的類,都要實現(xiàn)OnReceiveDataListener接口的抽象方法,同時PostRequest實例調(diào)用setOnReceiveDataListener方法,注冊該監(jiān)聽器。完整調(diào)用步驟如下:

1.創(chuàng)建PostRequest對象,實現(xiàn)onReceiveData接口,編寫自己的onReceiveData方法

2.注冊監(jiān)聽器

3.調(diào)用PostRequest的iniRequest方法,初始化本次request

4.調(diào)用PostRequest的execute方法

 可能的改進:

1.如果需要多個觀察者,可以把只能注冊單個監(jiān)聽器改為可以注冊多個監(jiān)聽器,維護一個監(jiān)聽器List。

2.如果需求比較簡單,并希望調(diào)用流程更簡潔,iniRequest和execute可以合并

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論