android url 中文请求,乱码解决

本文提供了一个解决HTTP请求过程中乱码问题的示例代码,通过设置请求头为application/x-www-form-urlencoded,并使用URLDecoder.decode进行中文内容的转码,确保了请求参数正确传递。

解决乱码的代码:

   1:      private String connServerForResult(String url) {
   2:          String strResult = "";
   3:          try {
   4:              HttpClient httpClient = new DefaultHttpClient();
   5:              HttpPost httpPost = new HttpPost();
   6:              httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
   7:              httpPost.setURI(new URI(url));
   8:              
   9:              //httpPost,通过urlParam添加请求参数
  10:              List<NameValuePair> urlParam = new ArrayList<NameValuePair>();
  11:              
  12:              //中文内容,通过URLDecoder.decode转码
  13:              urlParam.add(new BasicNameValuePair("date", URLDecoder.decode(date_search.getText().toString(), "utf-8")));
  14:              
  15:              httpPost.setEntity( new UrlEncodedFormEntity(urlParam, HTTP.UTF_8));
  16:              HttpResponse response = httpClient.execute(httpPost);
  17:              HttpEntity entity = response.getEntity();
  18:              if (entity != null)
  19:                  strResult = EntityUtils.toString(entity, HTTP.UTF_8);
  20:          } catch (Exception e) {
  21:          }
  22:          return strResult;
  23:      }

转载于:https://www.cnblogs.com/shuanger/archive/2012/12/10/2811715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值