zoukankan      html  css  js  c++  java
  • Android中文URL乱码问题 解决

    问题现象:在Android中通过HttpGet发送http请求时,url中的中文到了服务器就变成了乱码。代码如下:

    HttpClient client = new DefaultHttpClient();
    HttpParams httpParams = client.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 180000);
    HttpResponse response = client.execute(new HttpGet( "http://HakonZhao.com/FunnyList/a?name=小小研究院" ));
     
        解决:
        使用HttpPost来发送请求,并且用java.net.URLEncoder对中文字符进行编码,代码如下:
    HttpPost httpPost = new HttpPost(new URI("http://HakonZhao.com/FunnyList/a"));
    List<NameValuePair> urlParam = new ArrayList<NameValuePair>();
    urlParam.add(new BasicNameValuePair("name", URLEncoder.encode("小小研究院 ")));
    httpPost.setEntity( new UrlEncodedFormEntity(urlParam, HTTP.UTF_8));
    client.execute(httpPost);
        在服务器端需要用URLDecoder对中文参数进行解码,代码如下:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    String name = URLDecoder.decode( request.getParameter("name"), "UTF-8");
                    。。。
           }
  • 相关阅读:
    159
    158
    157
    156
    155
    高中生都能看懂的莫比乌斯反演
    洛谷 P4449 于神之怒加强版
    洛谷 P3455 [POI2007]ZAP-Queries
    洛谷 P1829 [国家集训队]Crash的数字表格 / JZPTAB
    Dirichlet卷积
  • 原文地址:https://www.cnblogs.com/ggzjj/p/2997418.html
Copyright © 2011-2022 走看看