android 中post上传数据返回值是乱码问题的解决

最近有一个需求,上传软件中用户的自定义内容。遇到一个问题,post 能传递上数据,但是上传的返回值却是乱码。

代码如下:

HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://xx.xx.xx.xx");
		httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
		httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("uid", "fff"));
			params.add(new BasicNameValuePair("action", "set"));
			params.add(new BasicNameValuePair("data", "112"));

			HttpEntity entity = new UrlEncodedFormEntity(params);
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity responseEntity = response.getEntity();
			if (responseEntity != null) {
				Log.i("~peter", EntityUtils.toString(responseEntity, "utf-8"));
			} else {
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			httpClient.getConnectionManager().shutdown();
		}

打印log 如下:

02-18 15:47:11.365: I/~peter(6092): ~peter �������������+)*M���L��������

后来找到原因:

原因在于代码中的这句:

httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");

在header 中 使用 Accept-Encoding字段,服务端默认使用了第一个gzip的压缩格式传输数据。所以会出现乱码。


两种解决方法:

1. 去掉这句 : httpPost.addHeader("Accept-Encoding""gzip,deflate,sdch");

2. 接收消息的地方解压一下。

		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://mm.maxthon.cn/mxbrowser/userdata.php");
		httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
		httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("uid", "fff"));
			params.add(new BasicNameValuePair("action", "set"));
			params.add(new BasicNameValuePair("data", "112"));

			HttpEntity entity = new UrlEncodedFormEntity(params);
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
//			HttpEntity responseEntity = response.getEntity();
//			if (responseEntity != null) {
//				Log.i("~peter", EntityUtils.toString(responseEntity, "utf-8"));
//			} else {
//				
//			}
			
			//解压返回数据
			byte[] body = EntityUtils.toByteArray(response.getEntity());
			if(MxHttpClient.isGzipContentEnc(response)){
				body = MxHttpClient.unzipData(body);
			}
			Log.i("~peter", "result=" + new String(body));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			httpClient.getConnectionManager().shutdown();
		}

	public static boolean isGzipContentEnc(HttpResponse response) {
		String contentEnc = null;
		Header header = response.getEntity().getContentEncoding();
		if (header != null) {
			contentEnc = header.getValue();
		}
		if (contentEnc != null && contentEnc.toLowerCase().indexOf("gzip") >= 0) {
			return true;
		}
		return false;
	}

	public static final byte[] unzipData(byte[] in) {
		ByteArrayInputStream bais = null;
		ByteArrayOutputStream baos = null;
		try {
			bais = new ByteArrayInputStream(in);
			baos = new ByteArrayOutputStream();
			unZipData(bais, baos);
				
		}// of try
		catch (Exception ioe) {
			ioe.printStackTrace();
			baos = null;
		} finally {
			try {
				if (bais != null)
					bais.close();
				if (baos != null)
					baos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	   
		return baos.toByteArray();
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值