Android客戶端請求伺服器端的詳細解釋

在南京看海發表於2016-09-09

Android客戶端請求伺服器端的詳細解釋

1. Android客戶端與伺服器端通訊方式:
Android與伺服器通訊通常採用HTTP通訊方式和Socket通訊方式,而HTTP通訊方式又分get和post兩種方式。
2. 解析伺服器端返回資料的解釋:
(1).
對於伺服器端來說,返回給客戶端的資料格式一般分為html、xml和json這三種格式。
(2). JSON(Javascript Object Notation)是一種輕量級的資料交換格式,相比於xml這種資料交換格式來說,因為解析xml比較的複雜,而且需要編寫大段的程式碼,所以客戶端和伺服器的資料交換格式往往通過JSON來進行交換。
3. Android中,用GET和POST訪問http資源
(1).
客戶端向伺服器端傳送請求的時候,向伺服器端傳送了一個資料塊,也就是請求資訊。
(2). GET和POST區別:
A: GET請求請提交的資料放置在HTTP請求協議頭(也就是url)中,而POST提交的資料則放在實體資料中,安全性比較高。
B: GET方式提交的資料最多隻能有1024位元組,而POST則沒有此限制。
這裡寫圖片描述
注意:考慮到POST的優勢,在Android開發中自己認為最好用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;

    /***
     * 客戶端呼叫的方法:傳遞引數向伺服器中傳送請求
     * 
     * @param userId
     * @param userName
     * @return
     */
    public static JSONObject getData(String userId, String userName) {
        int modelId = USER_ID;
        List list = new ArrayList();
        list.add(new BasicNameValuePair("userId", userId));
        list.add(new BasicNameValuePair("userName", userName));

        return doPost(modelId, list);
    }

    /**
     * 請求伺服器的方法
     * 
     * @param model
     * @param paramList
     * @return
     */
    private static JSONObject doPost(int model, List paramList) {

        // 1.建立請求物件
        HttpPost httpPost = new HttpPost(INNER_URL);
        // post請求方式資料放在實體類中
        HttpEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
            httpPost.setEntity(entity);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        // 2.建立客戶端物件
        HttpClient httpClient = new DefaultHttpClient();
        // 3.客戶端帶著請求物件請求伺服器端
        try {
            // 伺服器端返回請求的資料
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // 解析請求返回的資料
            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;

    }

}

轉自http://www.2cto.com/kf/201506/404113.html

相關文章