青青求实现登录功能

布局:

<EditText
        android:padding="10dp"
        android:background="@drawable/black_line"
        android:layout_margin="20dp"
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>
    <EditText
        android:inputType="numberPassword"
        android:padding="10dp"
        android:background="@drawable/black_line"
        android:layout_margin="20dp"
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"/>
    <Button
        android:layout_margin="10dp"
        android:id="@+id/log_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn"
        android:text="登录"/>
    <Button
        android:layout_margin="10dp"
        android:id="@+id/reg"
        android:background="@drawable/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"/>

java:

定义全局变量

OkHttpClient okHttpClient;
    Handler handler;
    EditText username,password;
    SharedPreferences sp ;
    String token;

开始请求:

 okHttpClient = new OkHttpClient.Builder().build();
        handler = new Handler(Looper.getMainLooper());
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        findViewById(R.id.log_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String getname = username.getText().toString();
                String getpassword = password.getText().toString();
                if (getname.isEmpty()){
                    Toast.makeText(LogingActivity.this, "请输入用户名",Toast.LENGTH_SHORT).show();
                }else if (getpassword.isEmpty()){
                    Toast.makeText(LogingActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }else {
                    Map<String,String> data = new HashMap<>();
                    data.put("username",getname);
                    data.put("password",getpassword);
                    String json = new Gson().toJson(data);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
                    Request request = new Request.Builder()
                            .url(Connect.Log)
                            .post(requestBody)
                            .build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                                final String data = response.body().string();
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        JSONObject jsonObject = null;
                                        try {
                                            jsonObject = new JSONObject(data);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        if (jsonObject.optString("code").equals("200")){
                                            token = jsonObject.optString("token");
                                            sp = getSharedPreferences("Myshuju",MODE_PRIVATE);
                                            SharedPreferences.Editor editor = sp.edit();
                                            editor.putString("token",token);
                                            editor.commit();
                                            startActivity(new Intent(LogingActivity.this,MainActivity.class));
                                            finish();
                                            Toast.makeText(LogingActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                                        }else {
                                            Toast.makeText(LogingActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                        }
                    });
                }
            }
        });

        findViewById(R.id.reg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LogingActivity.this,RegActivity.class));
            }
        });

注册——————

布局:

 <EditText
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:background="@drawable/black_line"
        android:id="@+id/regname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>
    <EditText
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:background="@drawable/black_line"
        android:inputType="numberPassword"
        android:id="@+id/regpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"/>
    <EditText
        android:background="@drawable/black_line"
        android:padding="10dp"
        android:layout_marginTop="10dp"
        android:id="@+id/tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="电话号"/>
    <RadioGroup
        android:id="@+id/reg_radio"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/nan"
            android:text="男"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>
        <RadioButton
            android:id="@+id/nv"
            android:text="女"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>

    </RadioGroup>
    <Button
        android:layout_margin="10dp"
        android:background="@drawable/btn"
        android:id="@+id/reg_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"/>

定义变量:

EditText username,password,tel;
    RadioGroup radioGroup;
    RadioButton radioButton;
    OkHttpClient okHttpClient;
    Handler handler;

开始注册:

username = findViewById(R.id.regname);
        password = findViewById(R.id.regpassword);
        tel = findViewById(R.id.tel);
        radioGroup = findViewById(R.id.reg_radio);

        okHttpClient = new OkHttpClient.Builder().build();
        handler = new Handler(Looper.getMainLooper());
        findViewById(R.id.reg_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String getname = username.getText().toString();
                String getpassword = password.getText().toString();
                String gettel = tel.getText().toString();
                radioButton = findViewById(radioGroup.getCheckedRadioButtonId());
                String getsex = radioButton.getText().toString();
                if (getname.isEmpty()){
                    Toast.makeText(RegActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if (getpassword.isEmpty()){
                    Toast.makeText(RegActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if (gettel.isEmpty()){
                    Toast.makeText(RegActivity.this, "请输入电话", Toast.LENGTH_SHORT).show();
                    return;
                }else if (getsex.isEmpty()){
                    Toast.makeText(RegActivity.this, "请选择性别", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Map<String,String> data = new HashMap<>();
                    data.put("userName",getname);
                    data.put("password",getpassword);
                    data.put("phonenumber",gettel);
                    data.put("sex",getsex);
                    String json = new Gson().toJson(data);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
                    Request request = new Request.Builder()
                            .url(Connect.Reg)
                            .post(requestBody)
                            .build();
                    Call call = okHttpClient.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            final String data = response.body().string();
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    JSONObject jsonObject = null;
                                    try {
                                        jsonObject = new JSONObject(data);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    if (jsonObject.optString("code").equals("200")){
                                        Toast.makeText(RegActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                                        startActivity(new Intent(RegActivity.this,LogingActivity.class));
                                    }else{
                                        Toast.makeText(RegActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值