Activity
public class MainActivity extends AppCompatActivity implements LoginView {
@BindView(R.id.edit_name)
EditText editName;
@BindView(R.id.edit_pwd)
EditText editPwd;
@BindView(R.id.but_login)
Button butLogin;
private LoginPresenter loginPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
loginPresenter=new LoginPresenter(this);
}
private String name;
private String pwd;
@OnClick(R.id.but_login)
public void onViewClicked() {
name=editName.getText().toString();
pwd=editPwd.getText().toString();
if (!name.isEmpty()&&!pwd.isEmpty()){
loginPresenter.logininpre(name,pwd);
Toast.makeText(MainActivity.this,name+pwd,Toast.LENGTH_LONG).show();
}
}
@Override
public void getViewData(LoginBean bean) {
Toast.makeText(this,bean.getMsg(),Toast.LENGTH_LONG).show();
}
@Override
public void Jump() {
Intent intent=new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (loginPresenter!=null){
loginPresenter.Destory();
}
}
}
MVP
写一个model接口
iModel
package com.example.yuekaolx1.LoginMVP.model;
import java.util.Map;
public interface iLoginModel {
void getData(String url, Map<String,String> parms,Cteanview cteanview);
interface Cteanview{
void ChengGong(Object oj);
void ShiBai();
}
}
在Model实现
Model
package com.example.yuekaolx1.LoginMVP.model;
import com.example.yuekaolx1.bean.LoginBean;
import com.example.yuekaolx1.network.HttpUtil;
import java.util.Map;
public class LoginModel implements iLoginModel{
@Override
public void getData(String url, Map<String, String> parms, final Cteanview cteanview) {
HttpUtil.getInstance().doPost(url,LoginBean.class,parms, new HttpUtil.NetCallBack() {
@Override
public void OnSuccess(Object oj) {
cteanview.ChengGong(oj);
}
@Override
public void OnFailure(Exception e) {
}
});
}
}
iPrecenter
public interface iLoginPresenter {
void logininpre(String name,String pwd);
}
Precenter
package com.example.yuekaolx1.LoginMVP.presenter;
import android.util.Log;
import android.widget.Toast;
import com.example.yuekaolx1.LoginMVP.model.LoginModel;
import com.example.yuekaolx1.LoginMVP.model.iLoginModel;
import com.example.yuekaolx1.MainActivity;
import com.example.yuekaolx1.bean.LoginBean;
import java.util.HashMap;
import java.util.Map;
public class LoginPresenter implements iLoginPresenter{
MainActivity mView;
private final LoginModel loginModel;
private Map<String, String> map;
String url=“http://www.zhaoapi.cn/user/login”;
public LoginPresenter(MainActivity mView) {
this.mView = mView;
loginModel = new LoginModel();
}
@Override
public void logininpre(String name, String pwd) {
map = new HashMap<>();
map.put("mobile",name);
map.put("password",pwd);
loginModel.getData(url, map, new iLoginModel.Cteanview() {
@Override
public void ChengGong(Object oj) {
LoginBean bean= (LoginBean) oj;
mView.getViewData(bean);
if (bean.getCode().equals("0")){
mView.Jump();
}
}
@Override
public void ShiBai() {
}
});
}
public void Destory(){
if (mView!=null){
mView=null;
}
}
}
View
package com.example.yuekaolx1.LoginMVP.view;
import com.example.yuekaolx1.bean.LoginBean;
public interface LoginView {
void getViewData(LoginBean bean);
void Jump();
}
Utils网络封装(POST 登录请求 GET 数据请求)
package com.example.yuekaolx1.network;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class HttpUtil {
private final OkHttpClient client;
private static HttpUtil instance;
private Handler handler=new Handler();
//拦截器
private Interceptor getAppInterceptor(){
Interceptor interceptor=new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
Log.e("+++++++++++++++","拦截前");
Response response=chain.proceed(request);
Log.e("+++++++++++++++","拦截后");
return response;
}
};
return interceptor;
}
//耗时操作
private HttpUtil(){
final File file = new File(Environment.getExternalStorageDirectory(), "cachell");
client = new OkHttpClient().newBuilder()
.readTimeout(3000,TimeUnit.SECONDS)
.connectTimeout(3000,TimeUnit.SECONDS)
.addInterceptor(getAppInterceptor())
.cache(new Cache(file,10*1024))
.build();
}
//单例模式
public static HttpUtil getInstance(){
if (instance==null){
synchronized (HttpUtil.class){
instance = new HttpUtil();
}
}
return instance;
}
public void doPost(String url, final Class clazz, Map<String, String> parms, final NetCallBack necallBack){
final FormBody.Builder body = new FormBody.Builder();
for (String key:parms.keySet()){
body.add(key,parms.get(key));
}
final Request request = new Request.Builder().get().url(url).build();
final Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
necallBack.OnFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s=response.body().string();
Gson gson=new Gson();
final Object oj = gson.fromJson(s, clazz);
handler.post(new Runnable() {
@Override
public void run() {
necallBack.OnSuccess(oj);
}
});
}
});
}
public void doGet(String url, final Class clazz, final NetCallBack netCallBack) {
// 2. 创建一个请求对象
Request request = new Request.Builder()
.get()
.url(url)
.build();
// 3. 创建出一个Call对象
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.OnFailure(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 注意:response.body()只能用一次
// Log.i(TAG, "onResponse: "+response.body().string());
String result = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(result, clazz);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.OnSuccess(o);
}
});
// Log.i(TAG, "onResponse: "+result);
}
});
}
public interface NetCallBack{
void OnSuccess(Object oj);
void OnFailure(Exception e);
}
}