If you are upgrading from OkHttp library from 1.x to 2.x, glaringly the OkHttpClient method "open" is missing. The below code will NOT compile.
OkHttpClient client = new OkHttpClient();
HttpURLConnection conn = client.open(url);
OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory.
OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.
OkHTTP is an open source project designed to be an efficient HTTP client. It supports the SPDY protocol.
OkHttpClient client = new OkHttpClient(); Request getRequest = new Request. Builder() . url("https://mytodoserver.com/todolist") . build(); client.
As per the official change log:
URLConnection support has moved to the okhttp-urlconnection module. If you're upgrading from 1.x, this change will impact you. You will need to add the okhttp-urlconnection module to your project and use the OkUrlFactory to create new instances of HttpURLConnection:
// OkHttp 1.x:
HttpURLConnection connection = client.open(url);
// OkHttp 2.x:
HttpURLConnection connection = new OkUrlFactory(client).open(url);
Just remember to add the dependency as below to the Gradle file.
compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With