欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVP原生登陆注册

程序员文章站 2022-04-18 17:25:40
...

首先需要两个接口

	//登录
    public final static String LOGIN_API="http://172.17.8.100/small/user/v1/login";
    //注册
    public final static String REG_API="http://172.17.8.100/small/user/v1/register";

创建一个登陆的bean包

public class LoginBean {
    public String message;
    public String status;

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

创建接口RequestCallBack

public interface RequestCallBack {
    void Failure(String string);
    void Success(LoginBean loginBean);
}

创建契约类

public interface LoadContract {
    //p层
    public abstract class LloadPresenter{
        public abstract void Login(HashMap<String,String>map);
        public abstract void reg(HashMap<String,String>map);
    }
    //m层
    interface LloadModel{
        void Login(HashMap<String,String> map, String string, RequestCallBack callBack);
    }
    //v层
    interface LloadView{
        void Failure(String string);
        void Success(LoginBean loginBean);
        void error(String error);
    }
}

创建model层

import android.os.Handler;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jiaoyibo.bawei.com.dengluzhucelianxi.bean.LoginBean;
import jiaoyibo.bawei.com.dengluzhucelianxi.contract.LoadContract;
import jiaoyibo.bawei.com.dengluzhucelianxi.net.RequestCallBack;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * <p>文件描述:<p>
 * <p>作者:JYB<p>
 * <p>创建时间:2019/3/5<p>
 * <p>更改时间:2019/3/5<p>
 * <p>版本号:1<p>
 */
public class LoadModel implements LoadContract.LloadModel {
    private Handler handler = new Handler();
    @Override
    public void Login(HashMap<String, String> map, final String string, final RequestCallBack callBack) {
        //创建OkHttpClient方法
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        //请求体
        FormBody.Builder builder = new FormBody.Builder();
        //for循环添加参数
        for(Map.Entry<String,String>p:map.entrySet()){
            builder.add(p.getKey(),p.getValue());
        }
        //创建Request
        Request request = new Request.Builder()
                .url(string)
                .post(builder.build())
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.Failure("网络连接超时");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                //判断响应码
                if(response.code()==200){
                    //解析数据的方法
                    getjson(result,callBack);
                }
            }

            private void getjson(String result, final RequestCallBack callBack) {
                final LoginBean loginBean = new Gson().fromJson(result, LoginBean.class);
                if(loginBean!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.Success(loginBean);
                        }
                    });
                }
            }
        });
    }
}

创建presenter层

import java.util.HashMap;

import jiaoyibo.bawei.com.dengluzhucelianxi.api.UserApi;
import jiaoyibo.bawei.com.dengluzhucelianxi.bean.LoginBean;
import jiaoyibo.bawei.com.dengluzhucelianxi.contract.LoadContract;
import jiaoyibo.bawei.com.dengluzhucelianxi.model.LoadModel;
import jiaoyibo.bawei.com.dengluzhucelianxi.net.RequestCallBack;
import jiaoyibo.bawei.com.dengluzhucelianxi.vadio.VadoiUtil;

/**
 * <p>文件描述:<p>
 * <p>作者:JYB<p>
 * <p>创建时间:2019/3/5<p>
 * <p>更改时间:2019/3/5<p>
 * <p>版本号:1<p>
 */
public class LoadPresenter extends LoadContract.LloadPresenter {
    private LoadContract.LloadView lloadView;
    private final LoadModel loadModel;

    public LoadPresenter(LoadContract.LloadView lloadView) {
        this.lloadView = lloadView;
        loadModel = new LoadModel();
    }

    @Override
    public void Login(HashMap<String, String> map) {
        String phone = map.get("phone");
        if(!VadoiUtil.isMobile(phone)){
            lloadView.error("手机号登陆不对");
            return;
        }
        loadModel.Login(map, UserApi.LOGIN_API, new RequestCallBack() {
            @Override
            public void Failure(String string) {
                if(lloadView!=null){
                    lloadView.Failure(string);
                }
            }

            @Override
            public void Success(LoginBean loginBean) {
                if(lloadView!=null){
                    lloadView.Success(loginBean);
                }
            }
        });
    }

    @Override
    public void reg(HashMap<String, String> map) {
        String phone = map.get("phone");
        if(!VadoiUtil.isMobile(phone)){
            lloadView.error("手机号登陆不对");
            return;
        }
        loadModel.Login(map, UserApi.REG_API, new RequestCallBack() {
            @Override
            public void Failure(String string) {
                if(lloadView!=null){
                    lloadView.Failure(string);
                }
            }

            @Override
            public void Success(LoginBean loginBean) {
                if(lloadView!=null){
                    lloadView.Success(loginBean);
                }
            }
        });
    }
}

使用第三方正则表达式

登陆页面的操作

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

import jiaoyibo.bawei.com.dengluzhucelianxi.bean.LoginBean;
import jiaoyibo.bawei.com.dengluzhucelianxi.contract.LoadContract;
import jiaoyibo.bawei.com.dengluzhucelianxi.presenter.LoadPresenter;

public class MainActivity extends AppCompatActivity implements LoadContract.LloadView {
    private EditText phone;
    private EditText pwd;
    private LoadPresenter loadPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    //登陆
    public void login(View view){
        HashMap<String,String> map = new HashMap<>();
        map.put("phone",phone.getText().toString());
        map.put("pwd",pwd.getText().toString());
        loadPresenter.Login(map);
    }
    //注册
    public void reg(View view){
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
    }
    private void initView() {
        phone = (EditText) findViewById(R.id.phone);
        pwd = (EditText) findViewById(R.id.pwd);
        loadPresenter = new LoadPresenter(this);
    }

    @Override
    public void Failure(String string) {
        Toast.makeText(this,string,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void Success(LoginBean loginBean) {
        Toast.makeText(this,loginBean.getMessage(),Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this,ShowActivity.class);
        startActivity(intent);
    }

    @Override
    public void error(String error) {
        Toast.makeText(this,error,Toast.LENGTH_SHORT).show();
    }
}

注册页面的操作

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

import jiaoyibo.bawei.com.dengluzhucelianxi.bean.LoginBean;
import jiaoyibo.bawei.com.dengluzhucelianxi.contract.LoadContract;
import jiaoyibo.bawei.com.dengluzhucelianxi.presenter.LoadPresenter;

public class Main2Activity extends AppCompatActivity implements LoadContract.LloadView {
    private EditText phone;
    private EditText pwd;
    private LoadPresenter loadPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        phone = (EditText) findViewById(R.id.phone);
        pwd = (EditText) findViewById(R.id.pwd);
        loadPresenter = new LoadPresenter(this);
    }
    //注册
    public void reg(View view){
        HashMap<String,String> map = new HashMap<>();
        map.put("phone",phone.getText().toString());
        map.put("pwd",pwd.getText().toString());
        loadPresenter.reg(map);
    }
    @Override
    public void Failure(String string) {
        Toast.makeText(this,string,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void Success(LoginBean loginBean) {
        Intent intent = new Intent(Main2Activity.this,MainActivity.class);
        startActivity(intent);
        Toast.makeText(this,loginBean.getMessage(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void error(String error) {
        Toast.makeText(this,error,Toast.LENGTH_SHORT).show();
    }
}

登陆页面的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:hint="请输入您的账号" />

            <EditText
                android:id="@+id/pwd"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:hint="请输入您的密码" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:onClick="login"
                    android:text="登录"
                    tools:ignore="OnClick" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="400dp"
                    android:onClick="reg"
                    android:text="注册"
                    tools:ignore="OnClick" />
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:layout_gravity="center"
                >
                <Button
                    android:onClick="qq"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="QQ登录"
                    tools:ignore="OnClick" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

注册页面的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/loading_02"
    tools:context=".Main2Activity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/phone"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:hint="请输入您的账号" />

            <EditText
                android:id="@+id/pwd"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:hint="请输入您的密码" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="100dp"
                android:layout_gravity="center"
                >
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="reg"
                    android:text="注册"
                    tools:ignore="OnClick" />
            </RelativeLayout>

        </LinearLayout>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>