毕设的服务器方面算是快做完了,然后今天把app登录补充了一下,碰到了许多bug,头大。
想着吃完晚饭总结一下,结果同学叫去吃鸡,因为有两个妹子,盛情难却,就陪他们打了两三把,好久不玩,手生,感觉菜了不少,新装备什么突击兵,工装兵,三级甲坏了还能修理,不用你拼死拼活的抢了,这游戏就变得没意思了啊。
哎,然后呢有个妹子打了两把说头晕,就没再继续了。
登录界面如下:
超级简单的一个登陆界面
主activity代码如下:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = carId.getText().toString().trim();
String word = carWord.getText().toString().trim();
if (id != null && id.length() == 11 && word != null) {
final String url = "http://-----lz/car/login?carId=" + id + "&carWord=" + word;
System.out.println("url:" + url);
new Thread(new Runnable() {
@Override
public void run() {
String s = MyPostUtils.sendPost(url, "");
System.out.println("此处是返回的参数:" + s);
/*判断返回的是否成功*/
if (s.equals("0")) {
/*登录成功跳转*/
Intent intent = new Intent(CarLogin.this, BeginActivity.class);
startActivity(intent);
} else {
Looper.prepare();
Toast.makeText(getApplicationContext(), "登录出错,检查账号密码!!", Toast.LENGTH_LONG).show();
Looper.loop();
}
}
}).start();
} else {
Toast.makeText(getApplicationContext(), "请输入正确格式账号密码!!", Toast.LENGTH_LONG).show();
}
}
});
服务器的响应:
/**
* 登录
* @return
*/
@ResponseBody
@RequestMapping("/login")
public String login(String carId, String carWord, HttpServletRequest request) {
Car login = carService.login(carId, carWord);
if (login == null) {
return "1";
} else {
/*上线*/
carService.upDown(1, carId);
return "0";
}
}
1.android-java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080)解决方法
在浏览器中输入http://localhost:8080/x没有问题,
在安卓中http为localhost:8080/x就会出错;
解决办法:
打开cmd窗口;输出ipconfig,拿到自己的ip代替localhost;
成功请求;
2.android.os.NetworkOnMainThreadException的异常
登录输入的账号在验证是进行http请求,是不能再主线程里发送的,需要新建一个线程。
new Thread(new Runnable() {
@Override
public void run() {
String s = MyPostUtils.sendPost(url, "");
System.out.println("此处是返回的参数:" + s);
/*判断返回的是否成功*/
if (s.equals("0")) {
/*登录成功跳转*/
Intent intent = new Intent(CarLogin.this, BeginActivity.class);
startActivity(intent);
} else {
Looper.prepare();
Toast.makeText(getApplicationContext(), "登录出错,检查账号密码!!", Toast.LENGTH_LONG).show();
Looper.loop();
}
}
}).start();
3.在子线程中使用Toast的时候,出现Force close
错误提示:
Can’t create handler inside thread that has not called Looper.prepare()
解决方法:
Looper.prepare();
Toast.makeText(getApplicationContext(), "登录出错,检查账号密码!!",Toast.LENGTH_LONG).show();
Looper.loop();
原因:
子线程其ThreadLoacl中没有设置过Looper,所以会抛出异常