MvpActivity.java
public class MvpActivity extends AppCompatActivity implements IView, View.OnClickListener {
private IPresenterImpl mPresenter;
private Button Send_Btn;
private TextView Get_Text;
private String mUrl = "http://www.zhaoapi.cn/user/reg?mobile=18210302397&password=123456";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvp2);
mPresenter = new IPresenterImpl(this);
initView();
}
private void initView() {
Send_Btn = (Button) findViewById(R.id.Send_Btn);
Get_Text = (TextView) findViewById(R.id.Get_Text);
Send_Btn.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.onDetach();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Send_Btn:
startRequest();
break;
default:
break;
}
}
/**
* 调用presenter方法,开始请求数据
*/
private void startRequest() {
mPresenter.startRequest(mUrl, null);
}
@Override
public void showResponseData(Object data) {
RegBean regBean = (RegBean) data;
Get_Text.setText(String.valueOf(regBean.getData()));
Toast.makeText(this, String.valueOf(regBean.getData()), Toast.LENGTH_SHORT).show();
}
}
IView.java
/**
* 抽取View实现接口
*/
public interface IView<T> {
/**
* 请求成功返回的数据
*/
void showResponseData(T data);
}
IPresenter.java
/**
* P层的接口
* 里面简单封装了一个开始请求网络 开始做任务
*/
public interface IPresenter {
void startRequest(String url,String params);
}
IPresenterImpl.java
/**
* iPresenter实现接口类
*/
public class IPresenterImpl implements IPresenter {
private IModelImpl model;
private IView iView;
public IPresenterImpl(IView iView) {
this.iView = iView;
model = new IModelImpl();
}
@Override
public void startRequest(String url, String params) {
Log.i("dj", "startRequest");
model.requestData(url, params, new MyCallBack() {
@Override
public void setData(Object data) {
iView.showResponseData(data);
}
});
}
public void onDetach(){
if (model!=null){
model = null;
}
if (iView!=null){
iView = null;
}
}
}
IModel.java
/**
* Model接口
*/
public interface IModel {
/**'
* 封装方法
* 第一个参数:接口地址
* 第二个参数:如果是post请求方式的话得传参数
* 第三个参数:处理完保存到接口
*/
void requestData(String url, String params, MyCallBack callBack);
}
IModelImpl.java
/**
* IModel接口实现类
* 执行网络请求,对返回的数据进行处理
*/
public class IModelImpl implements IModel {
private MyCallBack callBack;
/**
* handler处理返回消息
*/
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
RegBean regBean = (RegBean) msg.obj;
if(callBack != null) {
callBack.setData(regBean);
}
break;
case 1:
break;
default:
break;
}
}
};
/**
* 处理请求数据
* @param url 请求的url地址
* @param params 如果为post请求,则传入post参数
* @param callBack 回调
*/
@Override
public void requestData(final String url, String params, final MyCallBack callBack) {
this.callBack = callBack;
new Thread(new Runnable() {
@Override
public void run() {
try {
//执行网络请求
String jsonStr = HttpUtils.get(url);
Gson gson = new Gson();
RegBean regBean = gson.fromJson(jsonStr, RegBean.class);
//成功返回数据后,通过handler讲消息回给主线程
mHandler.sendMessage(mHandler.obtainMessage(0, regBean));
} catch (Exception e) {
//抓住各种崩溃,全部归于请求失败
e.printStackTrace();
mHandler.sendMessage(mHandler.obtainMessage(0, "错误"));
}
}
}).start();
}
}
MyCallBack.java
/**
* 接口保存Model层处理网络请求的值
* 声明泛型为T
* 通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换
* 因为“任意化”,所以在编译的时候不会报错,只有在执行的时候直接崩溃
*
* @param <T>
*/
public interface MyCallBack<T> {
void setData(T data);
}
HttpUtils.java
public class HttpUtils {
public static String get(String urlStr) throws Exception {
URL url = new URL(urlStr);
//由一个有效的网址服务返回这个对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
InputStream stream = connection.getInputStream();
String inputStr = getInputStr(stream);
return inputStr;
}
//把流转成字符串
private static String getInputStr(InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
}
return sb.toString();
}
}
RegBean.java
/**
* 注册用的对象
*/
public class RegBean<T> {
private String msg;
private String code;
private T data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}