用MVP做一个登录注册

先看效果



首先第一步还是先导入依赖

这次我用的是gson的依赖包

implementation files('libs/gson-2.3.1.jar')
网络权限
<uses-permission android:name="android.permission.INTERNET" />

------------------------------------------下面是布局----------------------------

activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:hint="请输入手机号" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:hint="请输入密码" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录" />

        <Button
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="注册" />
    </LinearLayout>
</LinearLayout>

activity_reg

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:hint="请输入手机号" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:hint="请输入密码" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录" />

        <Button
            android:id="@+id/reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="注册" />
    </LinearLayout>
</LinearLayout>

----------------------------下面就是代码,和MVP的分包-------------------
首先我们先创建好MVP的三个包以及要用的bean包

Model包下的LoginModel类
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import bwie.com.usercenter.bean.MyData;
/**
 * Created by BAIPEI on 2017/12/6.
 */

public class LoginModel {
    ModelCallPresenter modelCallPresenter;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            modelCallPresenter.success((String) msg.obj);
        }
    };

    public void Login(final String wz, final String phone, final String password,ModelCallPresenter modelCallPresenter) {
        this.modelCallPresenter = modelCallPresenter;
        new Thread(new Runnable() {
            @Override
            public void run() {
                BufferedReader br;
                try {
                    URL url = new URL("http://120.27.23.105/user/" + wz + "?mobile=" + phone + "&&password=" + password);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    int code = con.getResponseCode();
                    if (code == 200) {
                        br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        Gson gson = new Gson();
                        String data = gson.fromJson(br.readLine(), MyData.class).getMsg();
                        Message m = new Message();
                        m.obj = data;
                        handler.sendMessage(m);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface ModelCallPresenter {
        void success(String msg);
    }
}

presenter包下的LoginPresenter类
import bwie.com.usercenter.model.LoginModel;

/**
 * Created by BAIPEI on 2017/12/6.
 */

public class LoginPresenter {

    MainPresenter mainPresenter;
    private LoginModel loginModel;

    public LoginPresenter(MainPresenter mainPresenter){
        this.mainPresenter = mainPresenter;
        loginModel = new LoginModel();
    }

    public void loginClick(String wz,String phone,String password){
        loginModel.Login(wz,phone,password, new LoginModel.ModelCallPresenter() {
            @Override
            public void success(String msg) {
                mainPresenter.lClick(msg);
            }
        });
    }

    public void regClick(){
        mainPresenter.rClick();
    }

    public interface MainPresenter{
        void lClick(String msg);
        void rClick();
    }
}

下面就是view包下的MainActivity代码
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 bwie.com.usercenter.R;
import bwie.com.usercenter.presenter.LoginPresenter;

public class MainActivity extends AppCompatActivity implements LoginPresenter.MainPresenter{
    private EditText phone,password;
    private Button login,reg;
    private LoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phone = findViewById(R.id.phone);
        password =findViewById(R.id.password);
        login =findViewById(R.id.login);
        reg =findViewById(R.id.reg);
        loginPresenter = new LoginPresenter(this);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.loginClick("login", phone.getText().toString(), password.getText().toString());
            }
        });
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.regClick();
            }
        });

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

    @Override
    public void rClick() {
        startActivity(new Intent(MainActivity.this, RegActivity.class));
    }
}

view包下的RegActivity类
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 bwie.com.usercenter.R;
import bwie.com.usercenter.presenter.LoginPresenter;

public class RegActivity extends AppCompatActivity implements LoginPresenter.MainPresenter{
    private EditText rphone,rpassword;
    private Button lreg;
    private LoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        rphone = (EditText) findViewById(R.id.phone);
        rpassword = (EditText) findViewById(R.id.password);
        lreg = (Button) findViewById(R.id.reg);
        loginPresenter = new LoginPresenter(this);
        lreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.loginClick("reg", rphone.getText().toString(), rpassword.getText().toString());
            }
        });
    }
    @Override
    public void lClick(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void rClick() {

    }
}

下面就是bean包下的MyData类
public class MyData {

    /**
     * msg : 登录成功
     * code : 0
     * data : {"age":null,"appkey":"770f5b35194cb46c","appsecret":"DAF145434D87578718CC32A246C9F50D","createtime":"2017-11-07T18:36:43","email":null,"gender":null,"icon":null,"mobile":"18256189819","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","token":"ACC1909F8EC2AD669F9095D77C063381","uid":1648,"username":"18256189819"}
     */

    private String msg;
    private String code;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值