笔记:基于安卓的登录,以及进行http交互的错误

毕设的服务器方面算是快做完了,然后今天把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,所以会抛出异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值