MVP简单登录实现

1.依赖OKhttp网络请求

2.创建4个包


model view presenter bean


3.创建model和view的接口

public interface Iview {
    void success();
    void unsuccess(String error);
}
public interface Imodel {
    public String login(User user);
}

4.创建类实现接口

package model;

import android.util.Log;

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

import bean.User;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import tools.API;
import utils.OkHttp3Utils;

/**
 * Created by Administrator on 2017/11/1 0001.
 */

public class Usermodel implements Imodel{
    String code="";
    @Override
    public String login(final User user) {
        HashMap<String,String> map=new HashMap<>();
        map.put("mobile",user.getName());
        map.put("password",user.getPass());
        OkHttp3Utils.getInstance().doPost(API.LOGIN, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("main","callback thread id is "+Thread.currentThread().getId());
            }
        });
        return code;
    }
}
package view;

/**
 * Created by Administrator on 2017/11/1 0001.
 */

public class UserView implements Iview {
    @Override
    public void success() {

    }

    @Override
    public void unsuccess(String error) {

    }
}

5.p端实现关联

package presenter;

import bean.User;
import model.Usermodel;
import view.Iview;

/**
 * Created by Administrator on 2017/11/1 0001.
 */

public class my_presenter {
    private  final Iview iview;
    private final Usermodel usermodel;

    public my_presenter(Iview iview) {
        this.iview = iview;
        this.usermodel = new Usermodel();
    }
    public void login(final User user){
        String code=usermodel.login(user);
        if(code.equals("0")){
            iview.success();
        }else {
            iview.unsuccess(code);
        }
    }
}

6.mainactivity

package com.example.mvpdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import bean.User;
import presenter.my_presenter;
import view.Iview;

public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener{

    EditText name,pass;
    Button btn;
    my_presenter myPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name= (EditText) findViewById(R.id.name);
        pass= (EditText) findViewById(R.id.pass);
       findViewById(R.id.btn).setOnClickListener(this);
        myPresenter=new my_presenter(this);

    }

    @Override
    public void success() {
        Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void unsuccess(String error) {
        Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        String username=name.getText().toString().trim();
        String password=pass.getText().toString().trim();
        if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            Toast.makeText(MainActivity.this,"手机号和密码不能为空",Toast.LENGTH_SHORT).show();
        }
        myPresenter.login(new User(username,password));
    }
}
具体的xml和实体类这里我就不贴了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值