Android向服务器端发送json数据
- android 向服务器端发送json数据,本文讲解的知识点比较基础,如果你是大神,请直接关闭该网页,免得浪费你宝贵时间。 1.向服务器端发送json数据 关键代码:
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
源码:
view sourceprint?
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.