Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttpClient "open" method missing in v2.0

Tags:

android

okhttp

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);
like image 630
sivag1 Avatar asked Oct 23 '15 21:10

sivag1


People also ask

Should OkHttpClient be Singleton?

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.

What is OkHttpClient in Java?

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.

Is OkHttp open source?

OkHTTP is an open source project designed to be an efficient HTTP client. It supports the SPDY protocol.

How do I create an OkHttpClient?

OkHttpClient client = new OkHttpClient(); Request getRequest = new Request. Builder() . url("https://mytodoserver.com/todolist") . build(); client.


1 Answers

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'
like image 186
sivag1 Avatar answered Oct 02 '22 06:10

sivag1