MVP入门,接口回调两种实现,new接口和implement实现,okhttpclient



activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/login"
        android:text="login"
        />
</LinearLayout>

登录判断的接口

public interface LoginCallBack {
    //接口,,

    //手机号码为空
    public void phoneEmpty();
    //密码为空
    public void passwordEmpty();
    //登录为空
    public void loginSuccess(Object object);
    //登录失败
    public void loginFailed(int code);
}

model里面的接口

public interface ModelCallBack {
    //model的接口.,
    public void success(String data);
    public void failed(int code);
}

MainActivity里面的 UI(view)

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


    private LoginPresenter loginPresenter;
    private Button btn;
    private EditText password;
    private EditText phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        password = (EditText) findViewById(R.id.password);
        phone = (EditText) findViewById(R.id.phone);
        btn = (Button) findViewById(R.id.login);
        //因为这是view层,只能通过presenter来和model联系,所以需要new出presenter
        //new出presenter对象,因为实现了接口,所以需要传当前的上下文,也就是接口
        loginPresenter = new LoginPresenter(this);

        btn.setOnClickListener(this);
    }



    //登录按钮的点击事件
    public void onClick(View view) {

        //上面已经new出来了presenter
        //这是mainActivity只能写UI的代码,,而逻辑代码需要写在presenter里面,,类名.调用
        loginPresenter.loginPanduan(phone.getText().toString(),password.getText().toString());
    }

    //以下是实现接口重写的方法,
    @Override
    public void phoneEmpty() {
       //已经受presenter的loginPanduan方法里面逻辑代码的调用,吐司
        Toast.makeText(this, "phone is empty", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void passwordEmpty() {
        //已经受presenter的loginPanduan方法里面逻辑代码的调用,吐司
        Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginSuccess(final Object object) {
    //返回来的是子线程里的 需要开启子线程
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, object+"", Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void loginFailed(int code) {
        //返回来的是子线程里的 需要开启子线程
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "loginFailed", Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        loginPresenter.detach();
    }
}

Presenter 里面的 逻辑代码

public class LoginPresenter {
    //new出来model对象,presenter可以和model取得联系,model里面是请求网络数据的,数据库等等
     private LoginModel loginModel = new LoginModel();

    //当前传来的 参数是上下文,实现了接口以后,传来的就是接口
    LoginCallBack loginCallBack;
    public LoginPresenter(LoginCallBack loginCallBack) {
         this.loginCallBack = loginCallBack;
    }


    //presenter里面的方法
    public void loginPanduan(String phone, String password) {
        //这里面是逻辑代码,判断输入框是否合法
        if(TextUtils.isEmpty(phone)){
            //接口回调,调用avtivity也就是view里面已经实现了的接口的方法
            loginCallBack.phoneEmpty();
            return;
        }
        if(TextUtils.isEmpty(password)){
            loginCallBack.passwordEmpty();
            return;
        }

        //请求网络数据的方法在model里面,,类名·调用
        loginModel.getData(phone,password, new ModelCallBack() {
            @Override
            //重写modelCallBack里面2个的方法
            public void success(String data) {
               //拿到了model里面的okhttp,success里面返回的data是string数据
                //UI的改变需要在activity里面
                loginCallBack.loginSuccess(data);
            }

            @Override
            public void failed(int code) {
                //拿到了model里面okhttp,failed里面的返回的code是1
                //UI的改变需要在activity里面
                loginCallBack.loginFailed(code);
            }
        });
    }


    //防止内存泄露
    public void detach(){
        this.loginCallBack = null;
    }
}

Model里面的 请求网络数据,数据库的操作

public class LoginModel {

    //model里面是请求网络数据的方法,,,getData
    public void getData(String phone, String password, final ModelCallBack modelCallBack) {
        //使用OKHttpclient
        OkHttpClient client = new OkHttpClient();

        //post请求方式,需要传入一个requestbody
        RequestBody body = new FormBody.Builder()
                .add("pscid","39")
                .add("page","1")
                .build();

        Request request = new Request.Builder()
                .post(body)
                .url("http://120.27.23.105/product/getProducts")
                .build();

        //异步请求,,在子线程里
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //回调modelcallback里面的接口,,将1传过去presenter
                modelCallBack.failed(1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                //回调modelcallback里面的接口,,将请求到的数据传回去presenter
                modelCallBack.success(data);
            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值