OkHttpClient "public" method is missing in version 2.0

If you upgrade the OkHttp library from 1.x to 2.x, then there seems to be no OkHttpClient "open" method. The following code does NOT compile.

OkHttpClient client = new OkHttpClient(); HttpURLConnection conn = client.open(url); 
+5
source share
2 answers

According to the official change log :

URLConnection support has moved to the okhttp-urlconnection module. If you upgrade from 1.x, this change will affect you. You will need to add the okhttp-urlconnection module to your project and use 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); 

Remember to add the dependency as shown below to the Gradle file.

 compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0' 
+11
source

As with OkHttp 3.x, OkUrlFactory has been deprecated in favor of a new Request/Response call style, which is more flexible. Some information: https://publicobject.com/2015/12/15/okurlfactory-is-going-away/

So the new style will be more like:

 OkHttpClient httpClient = new OkHttpClient() Request request = Request.Builder() .url(url) .build() Response response = httpClient.newCall(request).execute() 
+1
source

Source: https://habr.com/ru/post/1234405/


All Articles