HttpUrlConnection背后的OkHttp

android4.4之后的HttpUrlConnection的實現(xiàn)是基于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,內(nèi)容如下:
rule com.squareup.** com.android.@1
rule okio.** com.android.okio.@1

jarjar 使用

從Android 4.4開始,HttpURLConnection的實現(xiàn)確實是通過調(diào)用okhttp完成的,而具體的方法則是通過HttpHandler這個橋梁,以及在OkHttpClient, HttpEngine中增加相應(yīng)的方法來實現(xiàn),當然,其實還涉及一些類的增加或刪除。

那我們來看看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背后隱藏的驚人真相

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,909評論 25 709
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,857評論 2 45
  • 感賞兒子和女兒今天在家沒有做出什么不良的行為、只是看電視。 感賞女兒今天認識了幾個新字。同時也很感賞兒子意識到自己...
    虹毅閱讀 238評論 0 0
  • 從現(xiàn)在開始,想要慢慢的學習寫一些東西。
    巧克力豆兒66閱讀 171評論 1 1
  • 從通道來到街上,正對面有家國營食堂,小時候覺得那里的包子挺大挺好吃,奇怪現(xiàn)在就不喜歡吃包子了。食堂旁邊是車站的倉庫...
    紅桃老k閱讀 378評論 0 1

友情鏈接更多精彩內(nèi)容