-
android 向服務器端發送json數據,本文講解的知識點比較基礎,如果你是大神,請直接關閉該網頁,免得浪費你寶貴時間。
1.向服務器端發送json數據
關鍵代碼:01.
public
void
sendJsonToServer() {
02.
HttpClient httpClient =
new
DefaultHttpClient();
03.
try
{
04.
05.
HttpPost httpPost =
new
HttpPost(constant.url);
06.
HttpParams httpParams =
new
BasicHttpParams();
07.
List<NameValuePair> nameValuePair =
new
ArrayList<NameValuePair>();
08.
Gson gson =
new
Gson();
09.
String str = gson.toJson(initData());
10.
nameValuePair.add(
new
BasicNameValuePair(
"jsonString"
, URLEncoder
11.
.encode(str,
"utf-8"
)));
12.
httpPost.setEntity(
new
UrlEncodedFormEntity(nameValuePair));
13.
httpPost.setParams(httpParams);
14.
Toast.makeText(Main.
this
,
"發送的數據:\n"
+ str.toString(),
15.
Toast.LENGTH_SHORT).show();
16.
httpClient.execute(httpPost);
17.
HttpResponse response = httpClient.execute(httpPost);
18.
StatusLine statusLine = response.getStatusLine();
19.
if
(statusLine !=
null
&& statusLine.getStatusCode() ==
200
) {
20.
HttpEntity entity = response.getEntity();
21.
if
(entity !=
null
) {
22.
Toast.makeText(
23.
Main.
this
,
24.
"服務器處理返回結果:"
+ readInputStream(entity.getContent()),
25.
Toast.LENGTH_SHORT).show();
26.
}
else
{
27.
Toast.makeText(Main.
this
,
"沒有返回相關數據"
, Toast.LENGTH_SHORT)
28.
.show();
29.
}
30.
}
else
{
31.
Toast.makeText(Main.
this
,
"發送失敗,可能服務器忙,請稍後再試"
,
32.
Toast.LENGTH_SHORT).show();
33.
}
34.
}
catch
(Exception e) {
35.
throw
new
RuntimeException(e);
36.
}
37.
}
38.
39.
private
static
String readInputStream(InputStream is)
throws
IOException {
40.
if
(is ==
null
)
41.
return
null
;
42.
ByteArrayOutputStream bout =
new
ByteArrayOutputStream();
43.
int
len =
0
;
44.
byte
[] buf =
new
byte
[
1024
];
45.
while
((len = is.read(buf)) != -
1
) {
46.
bout.write(buf,
0
, len);
47.
}
48.
is.close();
49.
return
URLDecoder.decode(
new
String(bout.toByteArray()),
"utf-8"
);
50.
}
51.
/*
52.
* 填充數據源
53.
*/
54.
public
List<Product> initData() {
55.
List<Product> persons =
new
ArrayList<Product>();
56.
for
(
int
i =
0
; i <
5
; i++) {
57.
Product p =
new
Product();
58.
p.setLocation(
"所在位置"
);
59.
p.setName(
"名稱"
+ i);
60.
persons.add(p);
61.
}
62.
return
persons;
63.
}
2.服務器端接收json數據後返回處理結果
3.利用Gson將集合轉換成json形式
如果你還沒有聽過gson 或是對其不是很熟悉,請先參考Android解析json數據(Gson),或是百度 谷歌之。4.服務器端採用VS建立一個網站,新建一個頁面androidtest.aspx
源碼:01.
protected
void
Page_Load(object sender, EventArgs e)
02.
{
03.
if
(Request[
"jsonString"
] !=
null
)
04.
{
05.
string json = Request[
"jsonString"
].ToString().Trim();
06.
json = HttpUtility.UrlDecode(json);
07.
try
08.
{
09.
string str = json.Substring(
0
, json.Length -
1
);
//去掉最後一個]
10.
str = str.Substring(
1
);
//去掉第一個[
11.
string[] sArray = Regex.Split(str,
"},"
);
12.
JavaScriptSerializer jss =
new
JavaScriptSerializer();
13.
for
(
int
i =
0
; i < sArray.Length; i++)
14.
{
15.
if
(i < sArray.Length -
1
)
16.
{
17.
sArray[i] +=
"}"
;
18.
}
19.
ProductBillList list = jss.Deserialize<ProductBillList>(sArray[i]);
20.
Response.Write(list.location + list.name +
"\n"
);
21.
}
22.
}
23.
catch
24.
{
25.
Response.Write(
"出現異常"
);
26.
}
27.
}
28.
else
29.
{
30.
Response.Write(
"接收數據失敗"
);
31.
}
32.
}
33.
public
class
ProductBill
34.
{
35.
public
List<ProductBillList> ProductBillLists { get; set; }
36.
}
37.
38.
public
class
ProductBillList
39.
{
40.
public
String name { get; set; }
41.
public
String location { get; set; }
42.
}
43.
效果: