Android 框架学习之 第一天 okhttp & Retrofit

最近面试,一直被问道新技术新框架,这块是短板,慢慢补吧。

关于框架的学习,分几个步骤

I.框架的使用

II.框架主流使用的版本和Android对应的版本

III.框架的衍生使用比如okhttp就会有Retrofit的使用

IV.框架历史版本,已经每个版本解决的问题

V.框架源码的分析

VI.框架设计思想,优缺点,如何解决。

第一天(20160919):

计划:

okhttp 的使用

okhttp 的主流版本和对应android版本

okhttp对应的retrofit的框架使用。 

 

okhttp:

GitHub地址:

https://github.com/square/okhttp

 

I.OKHttp的使用。

 下面是OKhttp的使用过程。

public class OkhttpRequestManagerImpl extends NetworkRequestBaseManager {
    OkHttpClient client = null;
    CallBackListener callBackListener = null;
    @Override
    public void initManager(NetWorkResponse response) {
        super.initManager(response);
        client = new OkHttpClient();
        callBackListener = new CallBackListener();
    }

    @Override
    public void release() {
        super.release();
        callBackListener = null;
        client = null;
    }

    @Override
    public void requestHttp(int method, String hostUrl, String methodUrl) {
        String url = hostUrl+methodUrl;
        final Request request = new okhttp3.Request.Builder().url(url)
                .addHeader("Accept", "application/json")
                .build();
        Call call = client.newCall(request);
        call.enqueue(callBackListener);
    }

    class CallBackListener implements okhttp3.Callback{

        @Override
        public void onFailure(Call call, IOException e) {
            deliverFailure(e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            deliverSuccess(response.body().string());
        }
    }
}

一个简单的使用过程如上代码。

post请求:

 @Override
    public void requestHttp(int method, String hostUrl, String methodUrl, Map<String,String> map) {
        switch(method)
        {
            case METHOD_GET:
                requestGet(hostUrl, methodUrl);
                break;
            case METHOD_POST:
                requestPost(hostUrl,methodUrl,map);
                break;
        }

    }

    private void requestPost(String hostUrl, String methodUrl, Map<String, String> map) {
        try {
            String url = hostUrl + methodUrl;
            StringBuilder tempParams = new StringBuilder();
            int pos = 0;
            for (String key : map.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(map.get(key), "utf-8")));
                pos++;
            }
            String params = tempParams.toString();
            RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, params);
            final Request request = new Request.Builder().url(url)
                    .post(body)
                    .build();
            Call call = client.newCall(request);
            call.enqueue(callBackListener);
        }catch (Exception e)
        {
            deliverFailure(e.getMessage());
        }
    }

    private void requestGet(String hostUrl, String methodUrl) {
        String url = hostUrl+methodUrl;
        final Request request = new Request.Builder().url(url)
                .addHeader("Accept", "application/json")
                .get()
                .build();
        Call call = client.newCall(request);
        call.enqueue(callBackListener);
    }

II.OKHttp 功能

 

posted on 2016-09-19 13:42  Joyfulmath  阅读(650)  评论(0编辑  收藏  举报

导航