HttpUrlConnection背后的OkHttp

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)的形式获得。但是通过类搜索方法并没有找到okhttp.HttpHandler。下面给出解析

JakeWharton okhttp开源作者
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

jarjar 使用

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

那我们来看看Android各版本引用的okhttp版本是多少呢!

  • Android 4.4.4_r1: 1.1.2

  • Android 5.0.1_r1: 2.0.0

  • Android 6.0.1_r1: 2.4.0

  • Android 7.1.0_r1: 2.6.0

参考 :Http(s)URLConnection背后隐藏的惊人真相

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,864评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,789评论 2 45
  • 感赏儿子和女儿今天在家没有做出什么不良的行为、只是看电视。 感赏女儿今天认识了几个新字。同时也很感赏儿子意识到自己...
    虹毅阅读 1,594评论 0 0
  • 从现在开始,想要慢慢的学习写一些东西。
    巧克力豆儿66阅读 1,292评论 1 1
  • 从通道来到街上,正对面有家国营食堂,小时候觉得那里的包子挺大挺好吃,奇怪现在就不喜欢吃包子了。食堂旁边是车站的仓库...
    红桃老k阅读 2,744评论 0 1

友情链接更多精彩内容