欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法

 更新時(shí)間:2015年12月29日 12:39:26   作者:q757989418  
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法,實(shí)例分析了Android針對(duì)HttpPost類(lèi)的操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法。分享給大家供大家參考,具體如下:

import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class RequestByHttpPost {
  public static String TIME_OUT = "操作超時(shí)";
  public static String doPost(List<NameValuePair> params,String url) throws Exception{
    String result = null;
     // 新建HttpPost對(duì)象
     HttpPost httpPost = new HttpPost(url);
     // 設(shè)置字符集
     HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
     // 設(shè)置參數(shù)實(shí)體
     httpPost.setEntity(entity);
     // 獲取HttpClient對(duì)象
     HttpClient httpClient = new DefaultHttpClient();
     //連接超時(shí)
     httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
     //請(qǐng)求超時(shí)
     httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
     try {
       // 獲取HttpResponse實(shí)例
      HttpResponse httpResp = httpClient.execute(httpPost);
      // 判斷是夠請(qǐng)求成功
      if (httpResp.getStatusLine().getStatusCode() == 200) {
        // 獲取返回的數(shù)據(jù)
        result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
        Log.i("HttpPost", "HttpPost方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:");
        Log.i("result", result);
      } else {
        Log.i("HttpPost", "HttpPost方式請(qǐng)求失敗");
      }
     } catch (ConnectTimeoutException e){
       result = TIME_OUT;
     }
     return result;
  }
}

可以直接用的完整類(lèi)。

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論