简单的MVP模式OkHttp网络请求框架封装

具体模式如图

160945_McTQ_3698786.png

准备工作

抽取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);
    }
}

不完善,等待更新

223916_bL9y_2663968.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值