Android中如何使用JSON来实现客户端和服务器的交互 第2页
用户向服务器发送请求,同时服务器反馈给手机端信息的:
1.packagecom.jclick.protocol;
2.
3.importjava.io.BufferedReader;
4.importjava.io.InputStreamReader;
5.importjava.util.ArrayList;
6.importjava.util.List;
7.
8.importorg.apache.http.HttpResponse;
9.importorg.apache.http.NameValuePair;
10.importorg.apache.http.client.HttpClient;
11.importorg.apache.http.client.entity.UrlEncodedFormEntity;
12.importorg.apache.http.client.methods.HttpPost;
13.importorg.apache.http.impl.client.DefaultHttpClient;
14.importorg.apache.http.message.BasicNameValuePair;
15.importorg.json.JSONException;
16.importorg.json.JSONObject;
17.
18.publicclassBaseProtocol{
19.privateStringBuildersb=newStringBuilder();
20.
21.privateHttpClienthttpClient;
22.privateHttpPosthttpRequest;
23.privateHttpResponseresponse;
24.
25.privateList<NameValuePair>nameValuePair=newArrayList<NameValuePair>();
26.
27.BaseProtocol(){
28.httpClient=newDefaultHttpClient();
29.}
30.
31./**
32.*向服务器端发送请求
33.*
34.*@paramurl 35.*@throwsException 36.*/
37.protectedvoidpack(Stringurl)throwsException{
38.httpClient=newDefaultHttpClient();
39.httpRequest=newHttpPost(url);
40.
41.httpRequest.setEntity(newUrlEncodedFormEntity(nameValuePair));
42.response=httpClient.execute(httpRequest);
43.}
44.
45./**
46.*得到返回数据
47.*
48.*@paramurl 49.*@return 50.*@throwsException 51.*/
52.protectedvoidparse()throwsException{
53.//TODO状态处理500200
54.if (response.getStatusLine().getStatusCode()== 200 ){
55.
56.BufferedReaderbufferedReader2=newBufferedReader(
57.newInputStreamReader(response.getEntity().getContent()));
58.for (Strings=bufferedReader2.readLine();s!= null ;s=bufferedReader2
59..readLine()){
60.sb.append(s);
61.}
62.}
63.}
64.
65./**
66.*向服务器发送信息
67.*
68.*@paramkey 69.*@paramvalue 70.*/
71.publicvoidaddNameValuePair(Stringkey,Stringvalue){
72.nameValuePair.add(newBasicNameValuePair(key,value));
73.}
74. 辣,文-论'文:网
http://www.751com.cn 75./**
76.*返回JSONObject对象数据模型
77.*
78.*@return 79.*@throwsJSONException 80.*/
81.publicJSONObjectgetJSON()throwsJSONException{
82.returnnewJSONObject(sb.toString());
83.}
84.
85.}
86.
87.packagecom.jclick.protocol;
88.
89.importjava.io.BufferedReader;
90.importjava.io.InputStreamReader;
91.importjava.util.ArrayList;
92.importjava.util.List;
93.
94.importorg.apache.http.HttpResponse;
95.importorg.apache.http.NameValuePair;
96.importorg.apache.http.client.HttpClient;
97.importorg.apache.http.client.entity.UrlEncodedFormEntity;
98.importorg.apache.http.client.methods.HttpPost;
99.importorg.apache.http.impl.client.DefaultHttpClient;
100.importorg.apache.http.message.BasicNameValuePair;
101.importorg.json.JSONException;
102.importorg.json.JSONObject;
103.
104.publicclassBaseProtocol{
105. privateStringBuildersb=newStringBuilder();
106.
107. privateHttpClienthttpClient;
108. privateHttpPosthttpRequest;
109. privateHttpResponseresponse;
110.
111. privateList<NameValuePair>nameValuePair=newArrayList<NameValuePair>();
112.
113. BaseProtocol(){
114. httpClient=newDefaultHttpClient();
上一页 [1] [2] [3] 下一页
Android中如何使用JSON来实现客户端和服务器的交互 第2页下载如图片无法显示或论文不完整,请联系qq752018766