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");
                。。。
       }
原文地址:https://www.cnblogs.com/ggzjj/p/2997418.html