Android端发送字符到Wed端,android应用端向web端获取字符串。

一 发送和接收普通的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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值