首先看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. PO1068 Parencodings 模拟题

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28860   Accepted: 16997 De ...

  2. php将用户信息提交到表单并且以txt文档打印出来

    1.分析: ====不推荐这种======== <?php function foo(){ // global $message; if(empty($_POST['username'])){ ...

  3. Python三十个常见的脚本汇总

    1.冒泡排序     2.计算x的n次方的方法     这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发.爬虫.数据分析.数据可视化.机器学习等.送给正在 ...

  4. 为什么wait()和notify()属于Object类

    关于wait()暂停的是持有锁的对象,所以想调用wait()必须为:对象.wait(); notify()唤醒的是等待锁的对象,调用:对象.notify(); 如下: Object obj = new ...

  5. AtomicInteger类的理解与使用

    AtomicInteger类的理解与使用 首先看两段代码,一段是Integer的,一段是AtomicInteger的,为以下: public class Sample1 { private stati ...

  6. Dream team: Stacking for combining classifiers梦之队:组合分类器

     sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  7. 面向对象【day08】:类的起源与metaclass(二)

    本节内容 1.概述 2.类的起源 3.__new__方法 4.__metaclass__方法 一.概述 前面我们学习了大篇幅的关于类,通过类创建对象,那我们想知道这个类到底是怎么产生的呢?它的一切来源 ...

  8. 设计模式---数据结构模式之迭代器模式(Iterate)

    一:概念 迭代模式是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator(遍历)按顺序进行遍历访问的设计模式. 在应用Iterator模式之前,首先应该明白Iterator ...

  9. 163邮箱SMTP设置

    如题要设置系统邮件自动发送 首先注册的网易邮箱要去开通SMTP服务,然后就是端口的设置 授权码 端口

  10. windows sqlplus客户端连接oralce数据库中文显示问题

    运行环境 服务器:centos6.8 服务器oracle版本:oracle 11g R2 64位,字符集是ZHS32utf8. 客户端:navicat 12x64  windows8.1x64 问题分 ...