Servlet在从Get请求中获取中文参数后,显示“?”
解决方法是:在Servlet获取的参数使用
URLDecoder.decode()进行编码;
下面是我的例子:传入的url为http://localhost:8080/test/RoomQueryServlet?mr_location=1楼101(浏览器直接输入该URL,Android客户端看最后)
1.改正前:
String mr_location = request.getParameter("mr_location");
System.out.println(mr_location);
打印的结果是:
1?101
2.改正后
String mr_location = request.getParameter("mr_location");
mr_location = URLDecoder.decode(mr_location,"UTF-8");
System.out.println(mr_location);
结果:
1楼101
关于Android客户端的传值例子如下,本来以为客户端也要使用URLEncoder.encode()进行编码,但是发现没有也可以成功:
以下是代码
//URL地址
String path = "http://"+IP+"/test/RoomQueryServlet";
path = path + "?mr_location=" + mr_location;
// path = URLEncoder.encode(path,"UTF-8");
Log.d("WEBSERVICE", "executeHttpGet: "+path);
connection = (HttpURLConnection) new URL(path).openConnection();
connection.setConnectTimeout(3000);//设置超时时间
connection.setReadTimeout(3000);
connection.setDoInput(true);
connection.setRequestMethod("GET");//设置获取信息方式
connection.setRequestProperty("Charset","UTF-8");//设置接收数据编码格式