Retrofit+MVP登录注册+EventBus
来源:互联网 发布:淘宝删除我的评价 编辑:程序博客网 时间:2024/05/17 15:04
//依赖
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);}//modelpublic 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); }}//presenterpublic 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); }}
//注册activitypublic 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登录 activitypublic 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; }}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
- Retrofit+MVP登录注册+EventBus
- Android mvp+Retrofit 登录注册
- MVP+Retrofit+fresco+greendao+eventbus
- [android架构篇]mvp+rxjava+retrofit+eventBus
- [android架构篇]mvp+rxjava+retrofit+eventBus
- android架构篇mvp+rxjava+retrofit+eventBus
- Retrofit+EventBus+GreenDao+MVP简单小总结
- MVP 实现登录注册
- 登录注册mvp模式
- MVP注册登录
- MVP 登录注册
- MVP注册登录
- MVP 登录+注册
- MVP登录注册
- MVP登录注册
- MVP登录注册
- Mvp实现登录注册
- MVP登录注册跳转
- IR cut filter 与 color shading
- Swift 扩展 Storyboard 属性
- Python基础-面向对象和面向过程编程区别
- B-Tree 、B+树、B*树
- RecyclerView多布局适配器
- Retrofit+MVP登录注册+EventBus
- C#网络编程系列文章(七)之UdpClient实现异步UDP服务器
- mt2503 [Editor]Email编辑界面字符显示滞后,跟不上手写速度
- 2D游戏新手引导点光源和类迷雾实现
- tensorflow实践(二) 基本原理学习和框架使用
- 文章标题
- stopPropagation()和preventDefault()
- 贾跃亭被法院列入“老赖”名单;360回应监控视频被直播;小米计划在印度扩展业务丨价值早报
- C#网络编程系列文章(八)之UdpClient实现同步UDP服务器