一、首先net包
1、OkHttpUtils 类
package com.example.mvpdemo.net; import java.io.IOException; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /** * Created by Administrator on 2018/4/16 0016. */ public class OkHttpUtils { private final OkHttpClient okHttpClient; private static OkHttpUtils okHttpUtils; private OkHttpUtils() { //创建OkHttpClient okHttpClient = new OkHttpClient(); } //懒汉式 public static OkHttpUtils getInstance(){ if (okHttpUtils==null){ okHttpUtils = new OkHttpUtils(); } return okHttpUtils; } public void login(String url, Map<String,String> params, final OnNetListener onNetListener){ if (params==null){ return; } //创建request对象 FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String,String> entry:params.entrySet()) { builder.add(entry.getKey(),entry.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder() .url(url) .post(formBody) .build(); //请求 okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { //失败 onNetListener.onFailed(e); } @Override public void onResponse(Call call, Response response) throws IOException { //成功 String string = response.body().string(); onNetListener.onSuccess(string); } }); } }2、 OnNetListener 接口
package com.example.mvpdemo.net; /** * Created by Administrator on 2018/4/16 0016. */ public interface OnNetListener { void onSuccess(String result); void onFailed(Exception e); }
3、url地址
public interface Api { public static final String LGOIN_URL = "https://www.zhaoapi.cn/user/login";
}二、model包
package com.example.mvpdemo.modle; import com.example.mvpdemo.net.OnNetListener; /** * Created by Administrator on 2018/4/16 0016. */ public interface LoginModel { void login(String name, String pass, OnNetListener onNetListener); }
package com.example.mvpdemo.modle; import com.example.mvpdemo.net.Api; import com.example.mvpdemo.net.OkHttpUtils; import com.example.mvpdemo.net.OnNetListener; import java.util.HashMap; import java.util.Map; /** * Created by Administrator on 2018/4/16 0016. */ public class LoginModelImp implements LoginModel{ @Override public void login(String name, String pass, OnNetListener onNetListener) { Map<String,String> params = new HashMap<>(); params.put("mobile",name); params.put("password",pass); OkHttpUtils.getInstance().login(Api.LGOIN_URL,params,onNetListener); }
三、presenter包
package com.example.mvpdemo.presenter; /** * Created by Administrator on 2018/4/16 0016. */ public interface LoginPresenter { void login(); }
package com.example.mvpdemo.presenter; import android.text.TextUtils; import com.example.mvpdemo.bean.UserBean; import com.example.mvpdemo.modle.LoginModelImp; import com.example.mvpdemo.net.OnNetListener; import com.example.mvpdemo.view.LoginView; import com.google.gson.Gson; /** * Created by Administrator on 2018/4/16 0016. */ public class LoginPresenterImp { private LoginView loginView; private final LoginModelImp loginModelImp; public LoginPresenterImp(LoginView loginView) { this.loginView=loginView; loginModelImp = new LoginModelImp(); } //登录 public void login(){ String name = loginView.getName(); String pass = loginView.getPass(); if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pass)){ loginView.showToast("输入的账号密码不正确"); return; } //进行网络请求 loginModelImp.login(name, pass, new OnNetListener() { @Override public void onSuccess(String result) { if (loginView==null){ return; } Gson gson = new Gson(); UserBean userBean = gson.fromJson(result, UserBean.class); loginView.showData(userBean.getData().getMobile()); } @Override public void onFailed(Exception e) { } }); } 四、view包
package com.example.mvpdemo.view; /** * Created by Administrator on 2018/4/16 0016. */ public interface LoginView { String getName(); String getPass(); void showData(String name); void showToast(String str); }
五、
package com.example.mvpdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.mvpdemo.presenter.LoginPresenterImp; import com.example.mvpdemo.view.LoginView; public class MainActivity extends AppCompatActivity implements LoginView{ private EditText et_name; private EditText et_pass; private Button login; private TextView tv; private LoginPresenterImp loginPresenterImp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = findViewById(R.id.et_name); et_pass = findViewById(R.id.et_pass); login = findViewById(R.id.login); tv = findViewById(R.id.tv); loginPresenterImp = new LoginPresenterImp(this); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loginPresenterImp.login(); } }); } @Override public String getName() { return et_name.getText().toString(); } @Override public String getPass() { return et_pass.getText().toString(); } @Override public void showData(final String name) { runOnUiThread(new Runnable() { @Override public void run() { tv.setText(name); } }); } @Override public void showToast(String str) { Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show(); } }