博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android4.4之后的HttpUrlConnection的实现是基于okhttp
阅读量:4519 次
发布时间:2019-06-08

本文共 3166 字,大约阅读时间需要 10 分钟。

首先看HttpUrlConnection使用

URL url = new URL("http://www.baidu.com");HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();urlConnection.setRequestMethod("get");urlConnection.connect();int responseCode = urlConnection.getResponseCode();

下面看URL类的源码实现

transient URLStreamHandler handler;public URL(String spec) throws MalformedURLException {        this(null, spec);    }public URL(URL context, String spec, URLStreamHandler handler)        throws MalformedURLException    {            //...            // Get the protocol handler if not specified or the protocol            // of the context could not be used            if (handler == null &&                (handler = getURLStreamHandler(protocol)) == null) {                throw new MalformedURLException("unknown protocol: "+protocol);            }            this.handler = handler;            //...    }public URLConnection openConnection() throws java.io.IOException {        return handler.openConnection(this);    }static URLStreamHandler getURLStreamHandler(String protocol) {        URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);        //...            // Fallback to built-in stream handler.            // Makes okhttp the default http/https handler            if (handler == null) {                try {                    if (protocol.equals("file")) {                        handler = (URLStreamHandler)Class.                            forName("sun.net.www.protocol.file.Handler").newInstance();                    } else if (protocol.equals("ftp")) {                        handler = (URLStreamHandler)Class.                            forName("sun.net.www.protocol.ftp.Handler").newInstance();                    } else if (protocol.equals("jar")) {                        handler = (URLStreamHandler)Class.                            forName("sun.net.www.protocol.jar.Handler").newInstance();                    } else if (protocol.equals("http")) {                        handler = (URLStreamHandler)Class.                            forName("com.android.okhttp.HttpHandler").newInstance();                    } else if (protocol.equals("https")) {                        handler = (URLStreamHandler)Class.                            forName("com.android.okhttp.HttpsHandler").newInstance();                    }                } catch (Exception e) {                    throw new AssertionError(e);                }            }            //...        }        return handler;    }

URL.openConnection()中的handler实例通过getURLStreamHandler方法中反射(com.android.okhttp.HttpHandler)的形式获得。

从Android 4.4开始,HttpURLConnection的实现确实是通过调用okhttp完成的,而具体的方法则是通过HttpHandler这个桥梁,以及在OkHttpClient, HttpEngine中增加相应的方法来实现,当然,其实还涉及一些类的增加或删除。

附:

但是通过类搜索方法并没有找到okhttp.HttpHandler。下面给出解析:

OkHttp in Android is here: https://android.googlesource.com/platform/external/okhttp/+/master. The reason the package name is com.android.okhttp is because there are jarjar rules which repackage it under that name.因为jarjar-rules的存在,其实路径为/external/okhttp/jarjar-rules.txt,内容如下:rule com.squareup.** com.android.@1rule okio.** com.android.okio.@1

 

转载于:https://www.cnblogs.com/genggeng/p/10103954.html

你可能感兴趣的文章
啊 不想要现在的自己
查看>>
11.13
查看>>
灭霸冲刺(4)
查看>>
测试理论学习
查看>>
【Ubuntu】下网络性能测试(转)
查看>>
Python-12:Python语法基础-函数
查看>>
Kali Linux Web渗透测试手册(第二版) - 1.1 - 渗透测试环境搭建
查看>>
Objects as Points
查看>>
126.输入输出深入以及小结
查看>>
泛型集合、泛型函数
查看>>
《掌握需求过程》阅读笔记03
查看>>
Redis详解(6)--redis持久化
查看>>
CMakeList.txt 案例
查看>>
【OpenCV学习】Kmean均值聚类对图片进行减色处理
查看>>
【IBM Tivoli Identity Manager 学习文档】5 管理员控制台
查看>>
12132
查看>>
js 操作select和option常见用法
查看>>
在SharePoint列表中使用自增栏
查看>>
[转]Resolving kernel symbols
查看>>
关于Lua 5.1中的debug.hook和coroutine
查看>>