第一步:
把Android端和web端的工程编码方式都改为utf-8
第二步:
在Android端对传递的参数进行如下处理
第三步:
在web端对接收的参数进行如下处理
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("urf-8");
同时一定要进行如下处理,如果不进行如下处理,还是会出错的,切记!!!
String start = request.getParameter("start");
String end = request.getParameter("end");
String end = new String(end.getBytes("ISO-8859-1"),"utf-8");
把Android端和web端的工程编码方式都改为utf-8
第二步:
在Android端对传递的参数进行如下处理
将要使用get方式传递的字符串处理一下在传递
start = URLEncoder.encode(start, "utf-8")
例如:
String path ="http://"+getpath+"/KuaiDi3/BusTime_Servlet?start="+URLEncoder.encode(start, "utf-8")+"&end="+URLEncoder.encode(end, "utf-8")+"&date="+URLEncoder.encode(date, "utf-8");
第三步:
在web端对接收的参数进行如下处理
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("urf-8");
同时一定要进行如下处理,如果不进行如下处理,还是会出错的,切记!!!
String start = request.getParameter("start");
String end = request.getParameter("end");
String date = request.getParameter("date");
String end = new String(end.getBytes("ISO-8859-1"),"utf-8");
String date = new String(date.getBytes("ISO-8859-1"),"utf-8");
方法封装:
android端:
/*
* 该方法把字符串转码为UTF_8
* 解决android端与web端通信字符串乱码问题
* */
public class StringTo_UTF_8 {
<span style="white-space:pre"> </span>public static String my_Encode(String instring) {
<span style="white-space:pre"> </span>String outString = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>outString = URLEncoder.encode(instring, "utf-8");
<span style="white-space:pre"> </span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return outString;
<span style="white-space:pre"> </span>}
}
web端:
public class Web_UTF_8 {
<span style="white-space:pre"> </span>public static String to_UTF_8(String inString){
<span style="white-space:pre"> </span>String outString = null;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>outString = new String(inString.getBytes("ISO-8859-1"),"utf-8");
<span style="white-space:pre"> </span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>};
<span style="white-space:pre"> </span>return outString;
<span style="white-space:pre"> </span>}
}