Retrofit+MVP登录注册+EventBus

//依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

compile 'org.greenrobot:eventbus:3.1.1'
//main_activity 登录
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.lenovo.retrofitlogin.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="登录"
        android:gravity="center"
        />
    <EditText
        android:id="@+id/et_sjh"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入手机号"
        />

    <EditText
        android:id="@+id/et_mm"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入密码"
        android:layout_marginTop="20dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <Button
            android:id="@+id/btn_dl"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="登录"
            />
        <Button
            android:id="@+id/btn_zc"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="注册"
            />
    </LinearLayout>

</LinearLayout>

//注册 activity_reg
<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="注册"
    android:gravity="center"
    />
<EditText
    android:id="@+id/reg_sjh"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:hint="请输入手机号"
    />

<EditText
    android:id="@+id/reg_mm"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:hint="请输入密码"
    android:layout_marginTop="20dp"
    />

<Button
    android:id="@+id/reg_zc"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="50dp"
    android:text="注册"
    />
//view层
public interface ILoginView {

    //登录成功
    void  onLogScuss(LoginBean bean);
    //注册成功
    void  onRegScuss(RegBean bean);

}
//model
public class LoginModel {

    ILoginModel model;

    public void setModel(ILoginModel model) {
        this.model = model;
    }

    //定义一个注册方法
    public void RegModel(final String name, final String pass){

        MyApp.inters.Regs(name,pass).enqueue(new Callback<RegBean>() {
            @Override
            public void onResponse(Call<RegBean> call, Response<RegBean> response) {

              RegBean bean=  response.body();
                model.reg(bean);

            }

            @Override
            public void onFailure(Call<RegBean> call, Throwable t) {


            }
        });

    }

    //定义一个登录方法
    public void LogModel(final String name, final String pass){

        MyApp.inters.login(name,pass).enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
              LoginBean bean=  response.body();
               model.login(bean);
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {

            }
        });


    }


        //定义一个接口类
    public interface ILoginModel {
        void  login(LoginBean bean);
        void reg(RegBean bean);
    }


}
//presenter
public class LoginPresenter implements LoginModel.ILoginModel{

    LoginModel model;
    ILoginView view;

    public LoginPresenter(ILoginView view) {
        this.view = view;
        model = new LoginModel();
        model.setModel(this);
    }


    @Override
    public void login(LoginBean bean) {
        view.onLogScuss(bean);
    }

    @Override
    public void reg(RegBean bean) {
        view.onRegScuss(bean);
    }

    //定义登录的方法
    public void LogPer(String name, String pass){
        model.LogModel(name,pass);
    }
    //定义注册的方法
    public void RecPer(String name,String pass){
        model.RegModel(name,pass);
    }
}

//接口
public interface Inters {

    //http://120.27.23.105/user/login?mobile=18765432100&password=666666
    @GET("user/login")
    Call<LoginBean>  login(@Query("mobile") String mobile, @Query("password") String password);

    //注册
    @GET("user/reg")
    Call<RegBean> Regs(@Query("mobile") String mobile, @Query("password") String password);

 }

//myapp 网络请求初始化
public class MyApp extends Application{
public  static  Inters inters;
    @Override
    public void onCreate() {
        super.onCreate();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://120.27.23.105/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

       inters= retrofit.create(Inters.class);

    }


}

//注册activity
public class Reg_Activity extends AppCompatActivity implements ILoginView {
    public EditText reg_sjh;
    public EditText reg_mm;
    public Button reg_zc;
    public LoginPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

//找控件
        reg_sjh = findViewById(R.id.reg_sjh);
        reg_mm = findViewById(R.id.reg_mm);
        reg_zc = findViewById(R.id.reg_zc);
        //拿到P层
        presenter = new LoginPresenter(this);

        //注册按钮点击事件
        reg_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(reg_sjh.getText().toString().trim().length()!=11){
                    Toast.makeText(Reg_Activity.this,"用户名输入错误",Toast.LENGTH_LONG).show();
                }else if(reg_mm.getText().toString().trim().length()!=6){
                    Toast.makeText(Reg_Activity.this,"密码输入错误",Toast.LENGTH_LONG).show();
                }else{
                    presenter.RecPer(reg_sjh.getText().toString(),reg_mm.getText().toString());
                }
            }
        });


    }


    @Override
    public void onLogScuss(LoginBean bean) {

    }

    @Override
    public void onRegScuss(RegBean bean) {
        if (bean.getCode().equals("0")) {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
        }

    }

}
  //eventbus传值bean
public class EventBusBean {

    private  String mobile;
    private String url;

    public EventBusBean(String mobile, String url) {
        this.mobile = mobile;
        this.url = url;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}



登录 activity
public class MainActivity extends AppCompatActivity implements ILoginView{
    public EditText et_sjh;
    public EditText et_mm;
    public Button btn_dl;
    public Button btn_zc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//找控件
        et_sjh = findViewById(R.id.et_sjh);
        et_mm = findViewById(R.id.et_mm);
        btn_dl = findViewById(R.id.btn_dl);
        btn_zc = findViewById(R.id.btn_zc);
        final LoginPresenter presenter = new LoginPresenter(this);

        //跳转到登录后主界面
        btn_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_sjh.getText().toString().trim().length()!=11){
                    Toast.makeText(MainActivity.this,"用户名输入错误", Toast.LENGTH_LONG).show();
                }else if(et_mm.getText().toString().trim().length()!=6){
                    Toast.makeText(MainActivity.this,"密码输入错误",Toast.LENGTH_LONG).show();
                }else{
                    presenter.LogPer(et_sjh.getText().toString(),et_mm.getText().toString());
                }
            }
        });
        //跳转到注册界面
        btn_zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Reg_Activity.class);
                startActivity(intent);
            }
        });


    }


    @Override
    public void onLogScuss(LoginBean bean) {
        if (bean.getCode().equals("0")){
            Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
             //EventBus传值
            EventBus.getDefault().postSticky(new EventBusBean(bean.getData().getUsername(),bean.getData().getIcon()+""));
            startActivity(new Intent(this,UserActivity.class));//登录成功跳转个人中心
            
        }else{
            Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onRegScuss(RegBean bean) {

    }


}
//个人中心
public class UserActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        EventBus.getDefault().register(this);
    }

    @Subscribe(sticky = true)
    public  void  event(EventBusBean busBean){

        Toast.makeText(this, ""+busBean.getUrl()+busBean.getMobile(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
//最后AndroidManifest.xml注册
android:name=".MyApp"




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值