首先看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的更多相关文章

  1. Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比

    比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有 ...

  2. 框架--NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用?

    NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用? NoHttp 源码及Demo托管在Github欢迎大家Star: https://github.com/Y0LANDA/NoH ...

  3. Okhttp【简介】应用 示例

    资源 GitHub:https://github.com/square/okhttp 官网     文档     API  You'll also need Okio[https://github.c ...

  4. Android开发的基础知识点

    1.Android开发的四大组件: Activity:android应用程序上看到的一页. Service:运行在后台,可以其他组件交互(音乐播放器). BroadcoastReceiver:用来对外 ...

  5. Android网络框架OkHttp之get请求(源码初识)

    概括 OkHttp现在很火呀.于是上个星期就一直在学习OkHttp框架,虽然说起来已经有点晚上手了,貌似是2013年就推出了.但是现在它版本更加稳定了呀.这不,说着说着,OkHttp3.3版本在这几天 ...

  6. 源码解析-Volley(转自codeKK)

    Volley 源码解析 本文为 Android 开源项目源码解析 中 Volley 部分项目地址:Volley,分析的版本:35ce778,Demo 地址:Volley Demo分析者:grumoon ...

  7. HTTP 2.0的那些事

    转自:http://www.admin10000.com/document/9310.html 在我们所处的互联网世界中,HTTP协议算得上是使用最广泛的网络协议.最近http2.0的诞生使得它再次互 ...

  8. Android 网络通信API的选择和实现实例

    Android开发网络通信一开始的时候使用的是AsyncTask封装HttpClient,没有使用原生的HttpURLConnection就跳到了Volley,随着OkHttp的流行又开始迁移到OkH ...

  9. java后台开发传输乱码&&接口post传参失败

    起因: 前几天遇到的问题,才有时间记录,需求:本地生成xml形式的字符串以参数形式用post方法传送到对方的固定接口: 这个需求写的时候感觉很容易,本地测试的时候,也觉得很简单就过了,然后和对方联调的 ...

随机推荐

  1. Luogu P2613 【模板】有理数取余

    题目链接 \(Click\) \(Here\) 真心没啥东西,只要能\(Get\)到在数字输入的时候按位取模,以及除数也可以直接取模就可以了.(把每个数看做乘法原理和加法原理构造起来的即可.) #in ...

  2. RJ45连接器

    http://www.huilyn.com/path315.html      HBJ-6308ANLF http://www.hanrun.com/en/           HR971169C h ...

  3. rownum查询前N条记录

    在Oracle中,要按特定条件查询前N条记录,用个rownum就搞定了.——select * from emp where rownum <= 5 而且书上也告诫,不能对rownum用" ...

  4. python学习笔记--文件重命名,删除及文件夹

    文件重命名 import os os.rename('123.txt','456.txt') 删除文件 >>> import os >>> os.remove('4 ...

  5. qml:基本知识记录

    1.  property信号处理函数: 在qml中,通过property可以定义属性,这些属性自带信号处理函数,例如: property   string    szTitle: “hello wor ...

  6. day14-jdbc案例(简单的curd&分页)

    回顾: mvc jsp的设计模式1: jsp+javabean jsp的设计模式2: jsp+javabean+servlet jsp:展示数据 javabean:封装数据 封装对数据的访问 serv ...

  7. Java基础构造方法和this关键字整理

    构造方法 8.1构造方法介绍 构造方法的格式: 修饰符 构造方法名(参数列表) { } l  构造方法的体现: n  构造方法没有返回值类型.也不需要写返回值.因为它是为构建对象的,对象创建完,方法就 ...

  8. Hadoop序列化-流量汇总案例

    Hadoop序列化案例-流量汇总需求 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Hadoop序列化 1>.为什么要序列化 一般来说,“活的”对象只生存在内存里,关机断 ...

  9. Silver 操作Cookie

    public class CookiesUtils { public static void SetCookie(String key, String value) { SetCookie(key, ...

  10. Openresty 学习笔记(四)lualocks包管理器安装使用

    Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖.安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比 ...