android-async-http将json封装到body体中

url直接拼接地址会超过限制,所以在开发中我们一般是将请求的参数写到body体中,下面我使用的是android的http网络框架,在实现的过程中出现了很多问题 ,现在代码记录:

 

 

 

 

   final byte[] zipSendDateService = zipSendDate(SelectSendDate().getBytes()); //上送的字符
		content = httpClient.getUploadSendData();//请求的报文
             //http的post请求,调用下面的post请求
            httpClient.post(context, content, zipSendDateService, 
		"application/octet-stream",new AsyncHttpResponseHandler() {
	@Override//请求成功
		public void onSuccess(int statusCode,Header[] headers, byte[] responseBody)             {
            processSendUploadResult(new String(responseBody));}

	@Override //请求失败
	public void onFailure(int statusCode,Header[] headers, byte[] responseBody,
	   Throwable error) {
    }
});

 

post的方式将字符串写到body体中请求服务器

 

//获得上面方法的参数	
public RequestHandle post(Context context, String content,
			byte[] zipSendDateService, String contentType,
			ResponseHandlerInterface responseHandler) {
//字节转成字节输入流
		ByteArrayInputStream bis = new ByteArrayInputStream(zipSendDateService);
将流写到Entity中
		InputStreamEntity entity = new InputStreamEntity(bis,
				zipSendDateService.length);
		try {
//转码
			content = URLEncoder.encode(content, HTTP.UTF_8);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//获得地址并且拼接json到url后面
		String url = getRequestUrl()+"?JSON="
				+ content;

//请求服务器
		return httpClient.post(context, url, entity, contentType,
				responseHandler);
	}

 

 

 

服务器可以直接读取body中的字符串

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值