#Android如何将utf-8编码为HttpPost?

时间:2015-12-04 06:21:50

标签: android encoding utf-8

如果要使用utf-8编码为POST方法发出HTTP请求服务器?

public **** makeHttpRequest(String url, String method, List<NameValuePair> params) {
   try{

       DefaultHttpClient httpClient = new DefaultHttpClient();
       HttpPost httpPost = new HttpPost(url);
       httpPost.setEntity(new UrlEncodedFormEntity(params));

       HttpResponse httpResponse = httpClient.execute(httpPost);
       HttpEntity httpEntity = httpResponse.getEntity();
       inputStream = httpEntity.getContent();

   } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
   } catch (ClientProtocolException e) {
        e.printStackTrace();
   } catch (IOException e) {
        e.printStackTrace();
   }

1 个答案:

答案 0 :(得分:0)

[解决] - 只需在URLEncodeFormEnitity方法中添加utf-8

  DefaultHttpClient httpClient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost(url);
  httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

  HttpResponse httpResponse = httpClient.execute(httpPost);
  HttpEntity httpEntity = httpResponse.getEntity();
  inputStream = httpEntity.getContent();