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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM