具体模式如图
准备工作
抽取Base类
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
initView();
initData();
}
protected abstract void initData();
protected abstract void initView();
protected abstract int getLayoutId();
@Override
protected void onResume() {
super.onResume();
AppUtil.mBaseActivity=this;
}
}
创建
public class AppUtil extends Application {
public static BaseActivity mBaseActivity;
@Override
public void onCreate() {
super.onCreate();
}
}
首先 别忘了导入依赖
compile 'com.squareup.okhttp3:okhttp:3.8.1'
然后:开启码农模式
第一步.V层 一个类足以
public interface IHomeView {
void getstringData(String data);
}
第二步.M层固定封装模式,可以按自身需求修改
1封装一个成功方法和失败方法的接口
public interface NetRequestClickListener {
void chenggong(String str);
void error(String e);
}
2封装OkHttp单例类
public class OkhttpUtil {
private static OkhttpUtil okhttpUtil;
private OkHttpClient okHttpClient;
private OkhttpUtil(){
okHttpClient=new OkHttpClient.Builder().build();
}
public static OkhttpUtil getInstance(){
if(okhttpUtil==null){
synchronized (OkhttpUtil.class){
if(okhttpUtil==null){
okhttpUtil=new OkhttpUtil();
}
}
}
return okhttpUtil;
}
public void get(String url, final NetRequestClickListener netRequestClickListener){
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
final String s = e.toString();
AppUtil.mBaseActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
netRequestClickListener.error(s);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
// Log.e("TAG",string+"-----------" );
AppUtil.mBaseActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
netRequestClickListener.chenggong(string);
}
});
}
});
}
}
3M层调用接口
public interface IHomeModel {
void requestData(String url, NetRequestClickListener netRequestClickListener);
}
4M实现类
public class HomeModel implements IHomeModel {
@Override
public void requestData(String url, NetRequestClickListener netRequestClickListener) {
OkhttpUtil.getInstance().get(url,netRequestClickListener);
}
}
第三步.P层实现类(沟通M.V)
public class HomePresenter implements NetRequestClickListener{
IHomeModel iHomeModel;
IHomeView iHomeView;
public HomePresenter(IHomeView iHomeView){
this.iHomeView=iHomeView;
iHomeModel=new HomeModel();
}
public void getUrl(String url){
iHomeModel.requestData(url,this);
}
@Override
public void chenggong(String str) {
iHomeView.getstringData(str);
}
@Override
public void error(String e) {
iHomeView.getstringData(e);
}
}
视图层显示使用
public class MainActivity extends BaseActivity implements IHomeView {
private TextView tv;
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void initData() {
}
@Override
protected void initView() {
tv = (TextView) findViewById(R.id.tv);
new HomePresenter(this).getUrl("http://www.ipanda.com/kehuduan/PAGE14501767715521482/index.json");
}
@Override
public void getstringData(String data) {
//具体使用网络请求回来的数据
Log.e("LAG", "--------------------" + data);
}
}
不完善,等待更新