android4.4之后的HttpUrlConnection的实现是基于okhttp
首先看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.@1
rule okio.** com.android.okio.@1
android4.4之后的HttpUrlConnection的实现是基于okhttp的更多相关文章
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有 ...
- 框架--NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用?
NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用? NoHttp 源码及Demo托管在Github欢迎大家Star: https://github.com/Y0LANDA/NoH ...
- Okhttp【简介】应用 示例
资源 GitHub:https://github.com/square/okhttp 官网 文档 API You'll also need Okio[https://github.c ...
- Android开发的基础知识点
1.Android开发的四大组件: Activity:android应用程序上看到的一页. Service:运行在后台,可以其他组件交互(音乐播放器). BroadcoastReceiver:用来对外 ...
- Android网络框架OkHttp之get请求(源码初识)
概括 OkHttp现在很火呀.于是上个星期就一直在学习OkHttp框架,虽然说起来已经有点晚上手了,貌似是2013年就推出了.但是现在它版本更加稳定了呀.这不,说着说着,OkHttp3.3版本在这几天 ...
- 源码解析-Volley(转自codeKK)
Volley 源码解析 本文为 Android 开源项目源码解析 中 Volley 部分项目地址:Volley,分析的版本:35ce778,Demo 地址:Volley Demo分析者:grumoon ...
- HTTP 2.0的那些事
转自:http://www.admin10000.com/document/9310.html 在我们所处的互联网世界中,HTTP协议算得上是使用最广泛的网络协议.最近http2.0的诞生使得它再次互 ...
- Android 网络通信API的选择和实现实例
Android开发网络通信一开始的时候使用的是AsyncTask封装HttpClient,没有使用原生的HttpURLConnection就跳到了Volley,随着OkHttp的流行又开始迁移到OkH ...
- java后台开发传输乱码&&接口post传参失败
起因: 前几天遇到的问题,才有时间记录,需求:本地生成xml形式的字符串以参数形式用post方法传送到对方的固定接口: 这个需求写的时候感觉很容易,本地测试的时候,也觉得很简单就过了,然后和对方联调的 ...
随机推荐
- hdu 3415"Max Sum of Max-K-sub-sequence"(单调队列)
传送门 题意: 给出一个有 N 个数字([-1000 , 1000],N ≤ 105)的环状序列: 让你求一个和最大的连续子序列,并记录起始点. 要求这个连续子序列的长度小于等于K,加和相同的不同区间 ...
- mysql 存储过程中文乱码的解决方法
CREATE PROCEDURE `PROC_FOOBAR`(id INTEGER) BEGIN DECLARE code, user_id VARCHAR(32) CHARACTER SET utf ...
- (数论 最大公约数 最小公倍数) codeVs 1012 最大公约数和最小公倍数问题
题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P,Q是正整 ...
- rabbitMQ 3.6.15生产环境
服务器配置 系统环境:CentOS 7 由于RabbitMQ依赖erlang, 所以需要先安装erlang 下载erlang 下载地址 http://erlang.org/download/ linu ...
- 离线提取目标机hash
##看了大佬的文章学习到一个姿势在此将其记录一下复现过程 github项目地址:https://github.com/SecureAuthCorp/impacket.git python setup ...
- OpenDayLight——HelloWorld
OpenDayLight——HelloWorld 既然学习OpenDayLight编程这个鬼,就得像学语言一样来一个HelloWorld来试试水,经过几天的折腾,总算成功输出HelloWorld了,这 ...
- jQuery_parent() parents() closest()区别
parent 是找当前元素的第一个父节点,不管匹不匹配都不继续往下找 parents 是找当前元素的所有父节点 closest 是找当前元素的所有父节点 ,直到找到第一个匹配的父节点 <%@ ...
- golang os包使用笔记
zhangsan os.Stidn 标准输入 os.Stdout 标准输出 os.Stderr 标准错误输出
- js方法参数中含有单引号双引号的处理
最近在做项目时,遇到一个问题.当在js脚本中,拼接生成一个tr,然后添加到一个表格里. //假定testval是从后台传过来的数据 var testval = "含有'半角单引号的字符串&q ...
- Spark MLlib 机器学习
本章导读 机器学习(machine learning, ML)是一门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多领域的交叉学科.ML专注于研究计算机模拟或实现人类的学习行为,以获取新知识.新 ...