GET方式提交数据
核心代码: 1、Android端 // 开启一个新的线程,发送请求到服务器
2、web端
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
username = new String(username.getBytes("iso8859-1"), "utf-8");
System.out.println("用户名:" + URLDecoder.decode(username,"utf-8"));
System.err.println("密码:" + password);
// 模拟查询数据库
if ("hacket".equals(username) && "123456".equals(password)) {
// 返回登陆成功
response.getOutputStream().write("登录成功".getBytes("utf-8"));
} else {
// 返回登陆失败
response.getOutputStream().write("登录失败".getBytes("utf-8"));
}
} |
android:
|
乱码1:response中的中文,出现的乱码,由于服务器和Android端的编码不一致 服务器:response.getOutputStream().write("登录成功".getBytes("utf-8")); Android客户端:new String(byteArray); |
乱码2:request提交中有中文,出现的乱码 1、服务端:会自动将中文数据url编码
GET /web/LoginServlet?username=%E5%B0%8F%E8%83%9C&password=123456 HTTP/1.1
username = new String(username.getBytes("iso8859-1"), "utf-8");
//但如果直接在浏览器中输入就会有乱码
System.out.println("用户名:" + URLDecoder.decode(username,"utf-8"));
2、android端提交:需要手动写 username = URLEncoder.encode(username, "utf-8");
String path = "http://192.168.221.221:8080/web/LoginServlet?username="+username+"&password="+password;
|