国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android客戶端post請求服務器端實例

2020-04-11 11:31:54
字體:
來源:轉載
供稿:網友

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

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

注意:考慮到POST的優(yōu)勢,在Android開發(fā)中自己認為最好用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;  /***   * 客戶端調用的方法:傳遞參數(shù)向服務器中發(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);  }  /**   * 請求服務器的方法   *    * @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ù)據放在實體類中    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.客戶端帶著請求對象請求服務器端    try {      // 服務器端返回請求的數(shù)據      HttpResponse httpResponse = httpClient.execute(httpPost);      // 解析請求返回的數(shù)據      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;  }}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 青州市| 石泉县| 遵义市| 孝昌县| 吴桥县| 隆化县| 定西市| 年辖:市辖区| 开阳县| 宜宾市| 德江县| 枝江市| 平南县| 简阳市| 琼结县| 射洪县| 松滋市| 红河县| 福安市| 即墨市| 汉阴县| 疏勒县| 元氏县| 濉溪县| 绿春县| 高陵县| 孟村| 正蓝旗| 砀山县| 新沂市| 莫力| 理塘县| 靖西县| 东乌珠穆沁旗| 北京市| 吴川市| 历史| 柳林县| 永济市| 沙湾县| 吴川市|