android登录注册功能基于springboot+mybatis后台

关于后台,可以看我的上一篇文章springboot+mybatis登录注册功能返回json
前台使用原生安卓运用Volley网络请求框架与后台交互
仅用于自己学习
页面和功能展示


界面ui参考一位博客主
这里只放一下请求后台的核心代码
注册发送请求的代码,使用volley post请求后台服务器根据后台返回的json数据来判断时候完成注册功能

 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = edt_username.getText().toString().trim();
                String password = edt_password.getText().toString().trim();
                String address = edt_address.getText().toString().trim();
                String phone = edt_phone.getText().toString().trim();
                if(username.equals("")||password.equals("")||address.equals("")||phone.equals("")){
                    Toast.makeText(RegistActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
                }else {
                    JSONObject jsonObject=new JSONObject();
                    try {
                        jsonObject.put("username",username);
                        jsonObject.put("password",password);
                        jsonObject.put("address",address);
                        jsonObject.put("phone",phone);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String url="http://192.168.1.103:8080/user/register";
                    RequestQueue requestQueue=Volley.newRequestQueue(RegistActivity.this);
                    JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            try {
                                Log.d("注册信息", jsonObject.toString());
                                String msg = jsonObject.getString("msg");
                                Toast.makeText(RegistActivity.this, msg, Toast.LENGTH_SHORT).show();
                                if(msg.equals("注册成功")){
                                    JSONObject detail = jsonObject.getJSONObject("detail");
                                   final String username_login = detail.getString("username");
                                    goLogin.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            Intent intent=new Intent(RegistActivity.this,MainActivity.class);
                                           intent.putExtra("username1",username_login);
                                           startActivity(intent);
                                        }
                                    });
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(RegistActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                        }
                    });
                    requestQueue.add(jsonObjectRequest);
                }
            }
        });

登录也是一样的将登录json发送post请求 通过后台返回的json判断密码是否正确

 btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usernameStr = login_username.getText().toString().trim();
                String passwordStr = login_password.getText().toString().trim();
                if(usernameStr.equals("")||passwordStr.equals("")){
                    Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    JSONObject jsonObject=new JSONObject();
                    try {
                        jsonObject.put("username",usernameStr);
                        jsonObject.put("password",passwordStr);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String url="http://192.168.1.103:8080/user/login";
                    RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
                    JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                           try {
                               Log.d("信息", jsonObject.toString());
                                String msg = jsonObject.getString("msg");
                               Log.d("msg", msg);
                                if(msg.equals("登录成功")){
                                    JSONObject detail = jsonObject.getJSONObject("detail");
                                    String username = detail.getString("username");
                                    Intent intent=new Intent(MainActivity.this,MenuActivity.class);
                                    intent.putExtra("username",username);
                                    startActivity(intent);
                                }else if(msg.equals("用户名或密码错误")){
                                    Toast.makeText(MainActivity.this, "用户名密码有误", Toast.LENGTH_SHORT).show();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                        }
                    });
                    requestQueue.add(jsonObjectRequest);
                }

            }
        });

登录注册Demo android代码
登录注册Demo springboot+mybatis代码

你可能感兴趣的:(android)