• 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 功能

  • 相关阅读:
    hive、sqoop、MySQL间的数据传递
    centos7配置Hadoop集群环境
    crontab定时时间解释
    Jmeter小技巧以及问题集合
    【总结】梳理下接口功能测试
    【部署问题】解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)
    【C#公共帮助类】 ToolsHelper帮助类
    【C#公共帮助类】枚举独特类
    【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(4)对前面的一些问题汇总和总结
    【C#公共帮助类】分页逻辑处理类
  • 原文地址:https://www.cnblogs.com/deman/p/5884874.html
Copyright © 2020-2023  润新知