利用MVP OkHttp网络 实现用户注册登陆功能

注册和登录整体框架是相似的 把url接口和传递参数换一下即可 所以只描述了注册的例子
在这里插入图片描述

OkHttp依赖

implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'com.squareup.okio:okio:1.7.0'

手机号正则表达式验证

package com.list.mvp.utils;
import android.text.TextUtils;
public class Utils {
    public static boolean isMobileNO(String mobileNums) {
        /**
         * 判断字符串是否符合手机号码格式
         * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
         * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
         * 电信号段: 133,149,153,170,173,177,180,181,189
         * @param str
         * @return 待检测的字符串
         */
        String telRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";// "[1]"代表下一位为数字可以是几,"[0-9]"代表可以为0-9中的一个,"[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\\d{9}"代表后面是可以是0~9的数字,有9位。
        if (TextUtils.isEmpty(mobileNums))
            return false;
        else
            return mobileNums.matches(telRegex);
    }
}

注册activity

	package com.list.mvp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.list.mvp.presenter.RegisterPresenter;
import com.list.mvp.utils.Utils;
import com.list.mvp.view.RegisterView;

public class RegistActivity extends AppCompatActivity implements RegisterView {

private EditText zhu_phone;
private EditText zhu_num;
private Button zhu_zhu;
private RegisterPresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_regist);
    //http://172.17.8.100/small/user/v1/register
    //初始化控件
    zhu_phone = findViewById(R.id.zhu_phone);
    zhu_num = findViewById(R.id.zhu_num);
    zhu_zhu = findViewById(R.id.zhu_zhu);

    presenter = new RegisterPresenter(this);

    //点击注册获取手机号密码之后进行校验
    zhu_zhu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //获取手机号
            String phone_zhu = zhu_phone.getText().toString().trim();
            String num_zhu = zhu_num.getText().toString().trim();
            //校验
            boolean mobileNO = Utils.isMobileNO(phone_zhu);
            if (!mobileNO) {
                Toast.makeText(RegistActivity.this, "注册手机号格式不对", Toast.LENGTH_SHORT).show();
                return;
            } else if (num_zhu.length() < 3) {
                Toast.makeText(RegistActivity.this, "注册密码格式不对", Toast.LENGTH_SHORT).show();
                return;
            }
            //发送参数
            presenter.sendParesenter(phone_zhu, num_zhu);
        }
    });
    //判断状态码:
}

//判断状态码
@Override
public void view(String message, String status) {
    if (status.equals("0000")){
       startActivity(new Intent(RegistActivity.this,MainActivity.class));
       finish();
    }
    Toast.makeText(RegistActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
}
}

RegisterModel

package com.list.mvp.model;
import android.os.Message;
import android.util.Log;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginModel {
    //定义接口
    public interface onLoginListener {
        void onResult(String message,String status);
    }

    //声明接口
    public onLoginListener loginListener;
//提供一个公共的设置监听的方法

public void setOnLoginListener(onLoginListener loginListener) {
    this.loginListener = loginListener;
}

private android.os.Handler handler = new android.os.Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0:
                String json = (String) msg.obj;
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    String message = jsonObject.getString("message");
                    String status = jsonObject.getString("status");

                    if (loginListener != null) {
                        //回调数据
                        loginListener.onResult(message,status);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
};

public void login(String phone, String num) {
    //创建网络请求对象
    OkHttpClient okHttpClient = new OkHttpClient
            .Builder()
            .connectTimeout(5000, TimeUnit.MILLISECONDS)
            .build();
    //创建requestBody并封装请求参数
    RequestBody requestBody = new FormBody
            .Builder()
            .add("phone", phone)
            .add("pwd", num)
            .build();
    //创建post请求
    Request request = new Request
            .Builder()
            .url("http://172.17.8.100/small/user/v1/login")
            .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 {
            String json = response.body().string();
            Log.i("xxxx",json);
            Message message = new Message();
            message.what = 0;
            message.obj = json;
            handler.sendMessage(message);
        }
    });
}
}

RegisterView

package com.list.mvp.view;

public interface RegisterView {
    void view(String message,String status);
}

RegisterParesenter

package com.list.mvp.presenter;
import com.list.mvp.model.RegisterModel;
import com.list.mvp.view.RegisterView;
public class RegisterPresenter {
 private RegisterModel registerModel;	
 private RegisterView RegisterView;

    //在构造方法中实例化对象
public RegisterPresenter(RegisterView view) {
    registerModel = new RegisterModel();
    RegisterView = view;
}

//传递过来的参数
public void sendParesenter(String phone_zhu, String num_zhu) {

    registerModel.regist(phone_zhu, num_zhu);
   //接收从Model回调过来的数据给view
    registerModel.setRegisterListener(new RegisterModel.OnRegisterListener() {
        @Override
        public void onResult(String message, String status) {
            //把数据传给view
            RegisterView.view(message,status);
        }
    });

}
}

register布局

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="注册"
    android:textSize="26sp" />

<EditText
    android:id="@+id/zhu_phone"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_margin="50dp"
    android:hint="请输入手机号"
    android:textSize="22sp" />

<EditText
    android:id="@+id/zhu_num"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_margin="50dp"
    android:hint="请输入密码"
    android:textSize="22sp" />

<Button
    android:id="@+id/zhu_zhu"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_below="@id/zhu_num"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="立即注册"
    android:textSize="22sp" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值