1.依赖OKhttp网络请求
2.创建4个包
model view presenter bean
3.创建model和view的接口
public interface Iview { void success(); void unsuccess(String error); }
public interface Imodel { public String login(User user); }
4.创建类实现接口
package model; import android.util.Log; import java.io.IOException; import java.util.HashMap; import bean.User; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; import tools.API; import utils.OkHttp3Utils; /** * Created by Administrator on 2017/11/1 0001. */ public class Usermodel implements Imodel{ String code=""; @Override public String login(final User user) { HashMap<String,String> map=new HashMap<>(); map.put("mobile",user.getName()); map.put("password",user.getPass()); OkHttp3Utils.getInstance().doPost(API.LOGIN, map, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Log.i("main","callback thread id is "+Thread.currentThread().getId()); } }); return code; } }
package view; /** * Created by Administrator on 2017/11/1 0001. */ public class UserView implements Iview { @Override public void success() { } @Override public void unsuccess(String error) { } }
5.p端实现关联
package presenter; import bean.User; import model.Usermodel; import view.Iview; /** * Created by Administrator on 2017/11/1 0001. */ public class my_presenter { private final Iview iview; private final Usermodel usermodel; public my_presenter(Iview iview) { this.iview = iview; this.usermodel = new Usermodel(); } public void login(final User user){ String code=usermodel.login(user); if(code.equals("0")){ iview.success(); }else { iview.unsuccess(code); } } }
6.mainactivity
package com.example.mvpdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import bean.User; import presenter.my_presenter; import view.Iview; public class MainActivity extends AppCompatActivity implements Iview,View.OnClickListener{ EditText name,pass; Button btn; my_presenter myPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name= (EditText) findViewById(R.id.name); pass= (EditText) findViewById(R.id.pass); findViewById(R.id.btn).setOnClickListener(this); myPresenter=new my_presenter(this); } @Override public void success() { Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); } @Override public void unsuccess(String error) { Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { String username=name.getText().toString().trim(); String password=pass.getText().toString().trim(); if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this,"手机号和密码不能为空",Toast.LENGTH_SHORT).show(); } myPresenter.login(new User(username,password)); } }具体的xml和实体类这里我就不贴了