一 发送和接收普通的JSONArray(仅仅包含字符串的数组)
1)web端:
PrintWriter out = response.getWriter();
List
orderAllList = new USER().getListStr();
JSONArray jsonArray =
JSONArray.fromObject(orderAllList);
System.out.println("wowowo"+jsonArray);
while (i!=4)
{//iter.hasNext()
out.write(jsonArray.toString());//发送的是json数组
}
2)android 端
HttpPost
request = new HttpPost(URL);
String sb = new String();
try {
HttpResponse response = new
DefaultHttpClient().execute(request);
HttpEntity he = response.getEntity();
sb=EntityUtils.toString(he, "utf-8");
JSONArray jsonArray =new JSONArray(sb);
System.out.println("#####"+jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
String temp = (String)
jsonArray.opt(i); System.out.println("------"+temp+"-----");
}
二 发送和接收复杂的json对象(包含指定的复杂对象数组)
1) web端
//数据封装
public List getListObj()
{
listObj = new
ArrayList();
USER user = null;
for (int i = 0; i
< 6; i++) {
user = new
USER();
user.setName("chenyb"
+ i);
user.setPassword("password"
+ i);
listObj.add(user);
}
return listObj;
}
//数据发送
List
orderAllList = new UserServiceImpl().getListObj();
JSONArray
jsonArray = JSONArray.fromObject(orderAllList);
System.out.println("***"+jsonArray);//json数据的特征,传递的原型
***[{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb0","password":"passwo
rd0"},{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb1","password":"passwor
d1"},{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb2","password":"password
2"},{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb3","password":"password3
"},{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb4","password":"password4"
},{"listStr":["chenyb0","chenyb1","chenyb2","chenyb3","chenyb4","chenyb5"],"name":"chenyb5","password":"password5"}
]
2) android端接收数据
HttpResponse response = new
DefaultHttpClient().execute(request);
HttpEntity he = response.getEntity();
sb=EntityUtils.toString(he, "utf-8");
JSONArray jsonArray =new JSONArray(sb);
System.out.println("#####"+jsonArray.toString());
System.out.println("#####"+jsonArray.toJSONObject(jsonArray));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject
temp = (JSONObject)jsonArray.opt(i);
System.out.println("------"+temp.getString("name")+"-----"+temp.getString("password"));
}
三 发送请求并且传递参数,根据指定参数来执行特定的方法
1) web端发送带参数的请求。
HttpPost request = new HttpPost(URL);
String sb = new String();
try {
List
params; params = new
ArrayList();
params.add(new BasicNameValuePair("flag",
"login")); HttpClient client =
HttpConnectUtil.getHttpClient();
request.setEntity(new UrlEncodedFormEntity(params,
HTTP.UTF_8));
HttpResponse respose =
client.execute(request);
HttpEntity he =
respose.getEntity(); sb=EntityUtils.toString(he, "utf-8");
JSONArray jsonArray =new JSONArray(sb);
System.out.println("#####"+jsonArray.toString());
System.out.println("#####"+jsonArray.toJSONObject(jsonArray));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject
temp = (JSONObject)jsonArray.opt(i);
System.out.println("------"+temp.getString("name")+"-----");
}
2)
请求端接收数据并返回数据
String
str = request.getParameter("flag");
System.out.println("##############"+str);
//发送数据代码通一二方法
//
设置网络的请求超时时间和等待超时时间,并返回客户端请求的对象。
BasicHttpParams httpParams = new
BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams,
SO_TIMEOUT);
HttpClient client = new
DefaultHttpClient(httpParams);