Android端发送字符到Wed端,android应用端向web端获取字符串。
一 發(fā)送和接收普通的JSONArray(僅僅包含字符串的數(shù)組)
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());//發(fā)送的是json數(shù)組
}
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+"-----");
}
二?發(fā)送和接收復(fù)雜的json對象(包含指定的復(fù)雜對象數(shù)組)
1) web端
//數(shù)據(jù)封裝
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;
}
//數(shù)據(jù)發(fā)送
List
orderAllList = new UserServiceImpl().getListObj();
JSONArray
jsonArray = JSONArray.fromObject(orderAllList);
System.out.println("***"+jsonArray);//json數(shù)據(jù)的特征,傳遞的原型
***[{"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端接收數(shù)據(jù)
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"));
}
三?發(fā)送請求并且傳遞參數(shù),根據(jù)指定參數(shù)來執(zhí)行特定的方法
1)?web端發(fā)送帶參數(shù)的請求。
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)
請求端接收數(shù)據(jù)并返回數(shù)據(jù)
String
str = request.getParameter("flag");
System.out.println("##############"+str);
//發(fā)送數(shù)據(jù)代碼通一二方法
//
設(shè)置網(wǎng)絡(luò)的請求超時時間和等待超時時間,并返回客戶端請求的對象。
BasicHttpParams httpParams = new
BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams,
SO_TIMEOUT);
HttpClient client = new
DefaultHttpClient(httpParams);
總結(jié)
以上是生活随笔為你收集整理的Android端发送字符到Wed端,android应用端向web端获取字符串。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql+json插入_MySQL对J
- 下一篇: android sina oauth2.