//第十三步 MainActivity实现IMainActivity接口
public class MainActivity extends AppCompatActivity implements IMainActivity{
private EditText edit_name;
private EditText edit_pwd;
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
edit_name = (EditText)findViewById(R.id.edit_name);
edit_pwd =(EditText) findViewById(R.id.edit_pwd);
//第一步:创建中间者对象
mainPresenter = new MainPresenter(this);//第十四步:在mainPresenter中写入this 注意:生成后MainPresenter中 public MainPresenter(IMainActivity iMainActivity) 要改里面的值
}
public void login(View view) {
String name = edit_name.getText().toString();
String pwd = edit_pwd.getText().toString();
//第二步:获取数据的逻辑就不再写到当前页面中了
mainPresenter.getLoginData("https://www.zhaoapi.cn/user/login",name,pwd);
}
@Override
public void loginSuccess(final LoginBean loginBean) {
// 第十六步:此时数据处于子线程....运行在主线程...runOnUiThread()是activity的方法,fragment中需要getActivity()进行使用
runOnUiThread(new Runnable() {
@Override
public void run() {
//设置数据的显示
Toast.makeText(MainActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
// int i= Integer.parseInt(dengluBean.getCode());
// if(i==1){
// //吐司
// Toast.makeText(DengluActivity.this,dengluBean.getMsg(),Toast.LENGTH_SHORT).show();
// }else{
//
// int code = Integer.parseInt(dengluBean.getCode());
// if(code==0){
// int uid = dengluBean.getData().getUid();
// String icon = (String) dengluBean.getData().getIcon();
// String username = dengluBean.getData().getUsername();
// // Toast.makeText(DengluActivity.this,dengluBean.getMsg(),Toast.LENGTH_SHORT).show();
// sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
// editor = sharedPreferences.edit();
// editor.putInt("uid",uid);
// editor.putString("icon",icon);
// editor.putString("username",username);
// editor.commit();
// Toast.makeText(DengluActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
// finish();
// }else{
// Toast.makeText(DengluActivity.this,dengluBean.getMsg(),Toast.LENGTH_SHORT).show();
// }
}
});
}
//第十七步:解除绑定.....内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
if (mainPresenter != null){
mainPresenter.destroy();
}
}
}
MainPresenter:
//第九步在MainPresenter实现IMainPresenter的接口
public class MainPresenter implements IMainPresenter {
private IMainActivity iMainActivity;
private MainModel mainModel;
public MainPresenter(IMainActivity iMainActivity) {
this.iMainActivity=iMainActivity;
//第四步:创建model
mainModel = new MainModel(this);//第十步:在mainModel中写入this
}
//第三步:中间者中获取数据的方法。。。。是由MainActivity中第二步生成的
public void getLoginData(String url, String name, String pwd) {
//第五步 中间者也不获取数据,,,,让model去获取数据...需要有一个model的引用,在中间者的构造方法中创建
mainModel.getLoginInModel(url,name,pwd);
}
@Override
public void onSuccess(LoginBean loginBean) {
//第十二步:创建view层的接口回调
// 第十五步:此时通过接口回调得到了model里面的数据...可以回调给view层
iMainActivity.loginSuccess(loginBean);
}
//第十八步:把view引用置空
public void destroy() {
if (iMainActivity != null){
iMainActivity = null;
}
}
}
MainModel:
public class MainModel {
private IMainPresenter iMainPresenter;
//接受的时候使用接口接受,是由第十步的this生成的 括号里要改成(我们写的接口名字)
public MainModel(IMainPresenter iMainPresenter) {
this.iMainPresenter=iMainPresenter;
}
//第六步,是由第五步生成的。。。。。真正获取数据的是model里面的方法
public void getLoginInModel(String url, String name, String pwd) {
//第七步:
Map<String, String> params=new HashMap<>();
params.put("mobile",name);
params.put("password",pwd);
OkHttp3Util.doPost(url, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
//获取json
String json=response.body().string();
//解析
Gson gson=new Gson();
LoginBean loginBean = gson.fromJson(json, LoginBean.class);
//第八步:开始写接口回调,先写presenter
//第十一步:回传数据。。。回调的数据仍然在子线程!!!!
iMainPresenter.onSuccess(loginBean);
}
}
});
}
}
presenter中的接口
//第八步:创建IMainPresenter接口
public interface IMainPresenter {
//参数类型就是传递的数据的类型
void onSuccess(LoginBean loginBean);
}
view中的接口
//第十二步:创建IMainActivity接口
public interface IMainActivity {
void loginSuccess(LoginBean loginBean);
}
MainActivity的布局
<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" tools:context="com.example.mvp_dome01.view.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#2e2d"
android:gravity="center"
android:text="登录页面" />
<EditText
android:hint="请输入用户名"
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="请输入密码"
android:id="@+id/edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:onClick="login"
android:text="登录"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:onClick="regist"
android:text="注册"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>