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請求的例子。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
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<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.創建請求對象 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 ; } } |