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方法传送到对方的固定接口: 这个需求写的时候感觉很容易,本地测试的时候,也觉得很简单就过了,然后和对方联调的 ...
随机推荐
- mongoDB-权限控制
启动服务D:\MongoDB\Server\3.6\bin>mongod.exe --dbpath D:\MongoDB\Server\3.6\data 扩展 无认证启动:mongod --po ...
- Linux 上 nginx配置
1:安装工具包 wget.vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake gcc g ...
- linux之/proc
/proc是Linux或UNIX中的一种伪文件系统机制,提供了访问内核运行结构.改变内核设置的实时数据. 与ext4.NFS.FAT32等文件系统不同,/proc中的数据存放在内存而不是硬盘上. 在/ ...
- 【知名的3D造型设计软件】犀牛 Rhinoceros 5.5.2 for Mac
[简介] 今天和大家分享最新的3D设计软件 犀牛 Rhinoceros for Mac 5.5.2 版本,支持中文界面,这是一款Mac上知名的3D造型软件,犀牛可以广泛地应用于三维动画制作.工业制造. ...
- CodeForces992E 二分 + 树状数组(线段树)
http://codeforces.com/problemset/problem/992/E 题意:给定一个序列 ai ,记其前缀和序列为 si ,有 q 个询问,每次单点修改,询问是否存在一个 ...
- hadoop 分布式开发环境搭建
一,安装java环境 添加java环境变量 vi /etc/profile # add by tank export JAVA_HOME=/data/soft/jdk/jdk1.7.0_71 ex ...
- 面向对象【day08】:异常处理-断言(七)
本节内容 1.概述 2.知识点回顾 3.断言 一.概述 python中断言,这个我是第一次听说到的,断言有什么用呢?断言就是做一些程序的检查工作,就是在执行之前需要做的一些检查,比如类似于安检一样,合 ...
- linq总结系列(一)---基础部分
一.linq的基本概念 LINQ是C#和VB中的统一查询语法,使用对象来保存和检索来自不同来源(如数据库.xml.对象集合)的数据. 主要功能:消除了编程语言和数据库之间的不匹配,以及为不同类型的数据 ...
- 关于CURL的初步认识
前言:你知道?这个CURL可以做爬虫的工作但是仅限于小型的爬虫.............. (一) CURL的认识 下面我觉得用图片来展示curl比较直观: 所谓的curl就是客户端访问了服务器1,那 ...
- SQL Server 调用 C# 方法实现正则表达式验证
Ø 前言 1. 在 SQL Server 中默认是不支持正则表达式验证的,如果需要某个字符串匹配一个正则表达式的验证规则,就需要额外的编写 C# 方法,并发布到 SQL Server 数据库中. ...