Android MVP(初级篇二、登陆案例)

2020-09-16  本文已影响0人  牧区叔叔

这篇文章仅仅是一个初识MVP的小白登陆项目总结,关于MVP知识点本片文章先不谈,通过本篇文章能简单应用达到效果即可。

效果:

(XML我没写Edittext,就一个Button应付了,吐司是响应成功后的动作)


image.png

复制粘贴即可

1、创建项目

2、创建包名

啥也不寻思先给他上一波包名(model、view、presenter、callback、api、bean)
image.png

3、给每个包创建相对应的java文件即可

image.png

4、看代码

(1)依赖
    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'//Retrofit依赖
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0' // 可选依赖,解析json字符所用
//rxjava
    implementation "io.reactivex.rxjava2:rxjava:2.1.3" // 必要rxjava2依赖
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1" // 必要rxandrroid依赖,切线程时需要用到
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' // 必要依赖,和Rxjava和Retrofit结合必须用到
(2)网络权限
  <uses-permission android:name="android.permission.INTERNET"/>
(3)api
//这里我用玩安卓登陆接口
//https://www.wanandroid.com/blog/show/2   自己看文档找登陆(自己先注册一个账号,一会会用到)
public interface LoginServer {
    String baseUrl = "https://www.wanandroid.com/user/";

    @POST("login")
    Observable<LoginBean> login(@Query("username")String username, @Query("password")String password);
}

(4)bean (自动生成)
image.png
(5)callback
/**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 11:05
 */
public interface LoginCallBack {
    void onSuccess(LoginBean loginBean);
    void onFail(String error);
}

(6)model

接口

/**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 10:58
 */
public interface LoginModel {
    void login(String usrname, String password, LoginCallBack loginCallBack);
}

实现类

/**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 11:27
 */
public class ImpLoginModel implements LoginModel {
    @Override
    public void login(String usrname, String password, final LoginCallBack loginCallBack) {
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(LoginServer.baseUrl)
                .build();
        //创建接口对象
        LoginServer loginServer = retrofit.create(LoginServer.class);
        Observable<LoginBean> login = loginServer.login(usrname, password);
        login
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {



                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        //获取的数值回传
                        loginCallBack.onSuccess(loginBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loginCallBack.onFail("net error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
(7)presenter

接口

/**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 13:54
 */
public interface LoginPresenter {
    void loginPresenter(String username,String password);
}

实现类

**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 13:54
 */
public class ImpLoginPresenter implements LoginPresenter, LoginCallBack {
    private ImpLoginModel impLoginModel;
    private LoginView loginView;

    public ImpLoginPresenter(ImpLoginModel impLoginModel, LoginView loginView) {
        this.impLoginModel = impLoginModel;
        this.loginView = loginView;
    }

    @Override
    public void loginPresenter(String username, String password) {
        if (impLoginModel!=null){
            impLoginModel.login(username,password,this);
        }
    }

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

    @Override
    public void onFail(String error) {
        if (loginView!=null){
            loginView.onFail(error);
        }
    }
}
(8)view

/**
 * @auther Eerdunsang
 * @date 2020/9/14
 * @time 14:01
 */
public interface LoginView {
    void onSuccess(LoginBean loginBean);
    void onFail(String error);
}
(9)xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">


    <Button
        android:id="@+id/login_bt"
        android:layout_centerInParent="true"
        android:text="登陆"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>
(10)activity
public class MainActivity extends AppCompatActivity implements LoginView {

    private Button loginBt;
    private  ImpLoginPresenter impLoginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        impLoginPresenter = new ImpLoginPresenter(new ImpLoginModel(), this);
    }
    
    private void initView() {
        loginBt = (Button) findViewById(R.id.login_bt);
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                impLoginPresenter.loginPresenter("这里填写申请账号","这里填写密码");
            }
        });
    }

    @Override
    public void onSuccess(LoginBean loginBean) {
//        Toast.makeText(this, loginBean.getData().getNickname(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFail(String error) {
        Toast.makeText(this, "请求失败", Toast.LENGTH_SHORT).show();
    }
}

到这里结束了,有问题留言。

上一篇下一篇

猜你喜欢

热点阅读