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

Android客戶端post請求服務(wù)器端實例

 更新時間:2015年06月05日 11:07:45   投稿:junjie  
這篇文章主要介紹了Android客戶端post請求服務(wù)器端實例,本文講解了Android客戶端與服務(wù)器端通信方式、解析服務(wù)器端返回數(shù)據(jù)的解釋、用GET和POST訪問http資源等內(nèi)容,并給出了一個POST實例,需要的朋友可以參考下

Android客戶端請求服務(wù)器端的詳細(xì)解釋

1. Android客戶端與服務(wù)器端通信方式:
Android與服務(wù)器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post兩種方式。
2. 解析服務(wù)器端返回數(shù)據(jù)的解釋:
(1).對于服務(wù)器端來說,返回給客戶端的數(shù)據(jù)格式一般分為html、xml和json這三種格式。
(2). JSON(Javascript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,相比于xml這種數(shù)據(jù)交換格式來說,因為解析xml比較的復(fù)雜,而且需要編寫大段的代碼,所以客戶端和服務(wù)器的數(shù)據(jù)交換格式往往通過JSON來進(jìn)行交換。
3. Android中,用GET和POST訪問http資源
(1).客戶端向服務(wù)器端發(fā)送請求的時候,向服務(wù)器端傳送了一個數(shù)據(jù)塊,也就是請求信息。
(2). GET和POST區(qū)別:
A: GET請求請?zhí)峤坏臄?shù)據(jù)放置在HTTP請求協(xié)議頭(也就是url)中,而POST提交的數(shù)據(jù)則放在實體數(shù)據(jù)中,安全性比較高。
B: GET方式提交的數(shù)據(jù)最多只能有1024字節(jié),而POST則沒有此限制。

注意:考慮到POST的優(yōu)勢,在Android開發(fā)中自己認(rèn)為最好用POST的請求方式,所以下面自己寫了一個小的POST請求的例子。代碼如下:

package com.scd.jsondemo.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {
  /** 地址 */
  private static final String INNER_URL = "http://localhost:8080/index2.jsp";
  /** TAG */
  private final String TAG = getClass().getSimpleName();
  private static final int USER_ID = 1;

  /***
   * 客戶端調(diào)用的方法:傳遞參數(shù)向服務(wù)器中發(fā)送請求
   * 
   * @param userId
   * @param userName
   * @return
   */
  public static JSONObject getData(String userId, String userName) {
    int modelId = USER_ID;
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(new BasicNameValuePair("userId", userId));
    list.add(new BasicNameValuePair("userName", userName));

    return doPost(modelId, list);
  }

  /**
   * 請求服務(wù)器的方法
   * 
   * @param model
   * @param paramList
   * @return
   */
  private static JSONObject doPost(int model, List<NameValuePair> paramList) {

    // 1.創(chuàng)建請求對象
    HttpPost httpPost = new HttpPost(INNER_URL);
    // post請求方式數(shù)據(jù)放在實體類中
    HttpEntity entity = null;
    try {
      entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
      httpPost.setEntity(entity);
    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
    }

    // 2.創(chuàng)建客戶端對象
    HttpClient httpClient = new DefaultHttpClient();
    // 3.客戶端帶著請求對象請求服務(wù)器端
    try {
      // 服務(wù)器端返回請求的數(shù)據(jù)
      HttpResponse httpResponse = httpClient.execute(httpPost);
      // 解析請求返回的數(shù)據(jù)
      if (httpResponse != null
          && httpResponse.getStatusLine().getStatusCode() == 200) {
        String element = EntityUtils.toString(httpResponse.getEntity(),
            HTTP.UTF_8);
        if (element.startsWith("{")) {
          try {
            return new JSONObject(element);
          } catch (JSONException e) {
            e.printStackTrace();
          }
        }
      }

    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;

  }

}

相關(guān)文章

最新評論