activity_main
<?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:orientation="vertical" android:layout_height="match_parent"> <EditText android:id="@+id/zh" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" /> <EditText android:id="@+id/mm" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码 " /> <Button android:id="@+id/dl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout>
MainActivity.java
package com.mvp; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity implements LoginView{ @BindView(R.id.zh) EditText zh; @BindView(R.id.mm) EditText mm; @BindView(R.id.dl) Button dl; private LoginPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); presenter = new LoginPresenter(this); } @OnClick(R.id.dl) public void onViewClicked() { //点击登录,调用LoginPresenter中的login方法 presenter.login(zh.getText().toString(),mm.getText().toString()); } @Override public void zhEmpty() { Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show(); } @Override public void mmEmpty() { Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show(); } @Override public void loginSuccess(Object object) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); } }); } @Override public void loginFailed(int code) { //Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show(); } }); } //页面关闭时调用 @Override protected void onDestroy() { super.onDestroy(); //调用LoginPresenter 里的detach方法,清空数据 presenter.detach(); } }
LoginView.java
package com.mvp; /** * Created by lenovo on 2017/11/02. */ public interface LoginView { // 手机号码为空 public void zhEmpty(); // 密码为空 public void mmEmpty(); // 登陆成功 public void loginSuccess(Object object); // 登陆失败 public void loginFailed(int code); }LoginModel.java
package com.mvp; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * Created by lenovo on 2017/11/02. */ public class LoginModel { public void login(String zh, String mm, final ModelCallBack callBack){ OkHttpClient okHttpClient = new OkHttpClient(); RequestBody body = new FormBody.Builder().add("pscid","39").add("page","1").build(); Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts").post(body).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { callBack.failed(1); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); callBack.success(result); } }); } interface ModelCallBack{ public void success(String data); public void failed(int code); } }LoginPresenter.java
package com.mvp; import android.text.TextUtils; /** * Created by lenovo on 2017/11/02. */ public class LoginPresenter { private LoginView loginView; private LoginModel loginModel; public LoginPresenter(LoginView loginView){ this.loginView = loginView; loginModel = new LoginModel(); } //清除数据 public void detach(){ this.loginView = null; } public void login(String zh,String mm){ if (TextUtils.isEmpty(zh)){ loginView.zhEmpty(); return; } if (TextUtils.isEmpty(mm)){ loginView.mmEmpty(); return; } loginModel.login(zh,mm, new LoginModel.ModelCallBack() { @Override public void success(String data) { loginView.loginSuccess(data); } @Override public void failed(int code) { loginView.loginFailed(code); } }); } }