功能
编写一个Android客户端,通过get/post方式登陆到远程服务器
- 输入
- 主机IP
- 用户名
- 密码
- 输出
- 登录状态
- 登陆的用户信息
Exception
-
IOException: Cleartext HTTP traffic to 192.168.1.1 not permitted
原因:没加权限 或 权限放的位置不对 -
点击post按钮,显示错误:
java.io.FileNotFoundException: http://192.168.1.106:8080/Android_ch6_test1_war_exploded/LoginServlet
原因:printWriter.write(paramsMap.toString());
paramsMap.toString() {password=123456,named=admin}
如果选择使用map来操作的话,可能应该使用这串代码(未验证)
(参考自 https://www.cnblogs.com/hailexuexi/p/3571853.html )
for(Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1); //删除最后的一个"&"
而我使用的是另一个方法,使用字符串类型变量,存储参数,缺点是没有map灵活,具有很大的局限性
String param = "username=" + name + "&" + "password=" + psd; //设置参数
printWriter.write(param); // 发送请求参数
附git地址:https://github.com/MelancholyCat/Classroom_Android/tree/master/ch6_test1