Relation between okhttp and HttpURLConnection

android, http, java, okhttp

Solution

And I find that HttpURLConnection's implement come from rt.jar which is from jdk.

No. Android does not use `rt.jar`, at compile time or at runtime.

okhttp is based on HttpURLConnection or HttpURLConnection is based on okhttp?

Android's concrete implementation of the `abstract` class named `HttpURLConnection` is based on a fork of OkHttp, as of Android 4.4.

`HttpURLConnection` itself is not tied to OkHttp; `HttpURLConnection` existed before Google or Square did. But `HttpURLConnection` is an `abstract` class — it is useless on its own. A Java runtime library needs a concrete implementation of `HttpURLConnection`, which it can then use to implement methods like `openConnection()` on `URL`, which needs to return some `HttpURLConnection` implementation.

FWIW, in Android 4.3 and older, the concrete implementation of `HttpURLConnection`, AFAIK, was based on the Apache Harmony implementation, the way that most `java.*` and `javax.*` classes in Android were.

Problem

Many people talk to me that HttpURLConnection is based on okhttp from android 4.4 on. But when I dive into codes of okhttp, the OkHttpURLConnection is a subclass extended from HttpURLConnection. So, okhttp is based on HttpURLConnection or HttpURLConnection is based on okhttp? And I find that HttpURLConnection's implement come from rt.jar which is from jdk. I hope someone can tell me the real relation between okhttp and HttpURLConnection

Original source