android post 服务器参数设置,android设置post请求服务器时参数的设置以及JSON参数的设置...

android  post的数据的时候,一些参数设置如下:

ObjectMapper mapper = new ObjectMapper();

Map paramsList = new HashMap();

HttpPost requestPost = new HttpPost(url);

requestPost.setHeader("charset", HTTP.UTF_8);

//httppost.addHeader("Content-Type", "application/json");

//requestPost.addRequestHeader("Content-Type","text/html;charset=UTF-8");

/*//封装JSON对象

JSONObject paramsList = new JSONObject();*/

int[] promotions = {1,2};

paramsList.put("shop_id", "14");

paramsList.put("order_id", "77");

paramsList.put("promotion", promotions);

paramsList.put("feedbackInfo", "有空调,有免费的WiFi哦");

paramsList.put("feedbackTime", "1404882661000");

content = mapper.writeValueAsString(paramsList);

//绑定到请求 Entry

StringEntity se = new StringEntity(content, HTTP.UTF_8");//paramsList.toString()

requestPost.setEntity(se);

//放松请求

HttpResponse httpResponse = new DefaultHttpClient().execute(requestPost);

if (httpResponse.getStatusLine().getStatusCode() == 200) { // 可能空指针

String response = EntityUtils.toString(

httpResponse.getEntity(), HTTP.UTF_8);

Log.d(LOG_TAG, response);

return response; }

ObjectMapper是开源框架jackson

因为post没法直接添加数组,所以可以使用ObjectMapper.writeValueAsString()方法是把JSONObject转化成字符串的方法。

下面的参数设置较简单,只是一般JSON的设置:

HttpPost request = new HttpPost(url);

// 先封装一个 JSON 对象

JSONObject param = new JSONObject();

param.put("name", "rarnu");

param.put("password", "123456");

// 绑定到请求 Entry

StringEntity se = new StringEntity(param.toString());

request.setEntity(se);

// 发送请求

HttpResponse httpResponse = new DefaultHttpClient().execute(request);

// 得到应答的字符串,这也是一个 JSON 格式保存的数据

String retSrc = EntityUtils.toString(httpResponse.getEntity());

// 生成 JSON 对象

JSONObject result = new JSONObject( retSrc);

String token = result.get("token");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值