Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why developers set OkHttp to Retrofit if Retrofit already uses OkHttp internally

I know, that Retrofit uses OkHttp internally. But, I can see that some developers provided applying of approach as bellow

return new Retrofit.Builder()
        .baseUrl(BuildConfig.API_ENDPOINT)
        .client(getClient()) // we can add OkHttpClient client there

So, can someone clarify why this needed?

I have heard that this can be helpful for testing, when we can apply custom Interceptor during mocking of HTTP requests

like image 965
Дмитро Яковлєв Avatar asked Aug 30 '17 12:08

Дмитро Яковлєв


People also ask

Why do we need OkHttp with retrofit?

OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests and responses manipulation. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. Retrofit is strongly coupled with OkHttp and makes intensive use of it.

Which is better OkHttp or retrofit?

You should use retrofit if you are trying to map your server API inside your application (type-safing). Retrofit is just an API adapter wrapped over okHTTP. If you want to type safe and modularise the interaction code with your API, use retrofit.

Does retrofit uses OkHttp?

It started as a component of OkHttp. Retrofit is a type-safe REST client for Java and Android application development. It consists of interfaces, classes, and methods that provide the required functionalities. JSON or XML data may be parsed and converted to POJOs (Plain Old Java Objects) using the Retrofit library.

Why do we use OkHttp in Android?

OkHttp android provides an implementation of HttpURLConnection and Apache Client interfaces by working directly on a top of java Socket without using any extra dependencies.


2 Answers

Retrofit does get an instance of OkHttp by itself but does not perform any customization of the OkHttpClient. In a lot of cases, you might need to customize you client to take advantage of the flexibility of OkHttp. Take a look at what you can do in this JavaDoc. You can see that you can do a lot of things like set your own timeout, your own DNS, your own custom cache (could come in handy in graphics intensive apps), your own proxy, limit protocols based on user device (a country might block HTTPS so you want to be able to fall back to HTTP in this case as quickly as possible) and many more.

EDIT: In most usage cases this might not be needed such as in a simple API call to a REST API endpoint. But in some cases, such as video streaming, VPN or proxy services, or whatever case that requires you to customize your app to geographical regions or different network connections you could benefit from this. Other examples that come to mind are some messaging or social apps that are blocked by certain countries. Even an app like Spotify or YouTube. When the user is connected through WiFi you want to route them to your high speed server through a high speed protocol (say UDP). But if the user is connected via 3G you want to route them to a different server and using TCP protocol to ensure quality.

like image 88
SoroushA Avatar answered Nov 10 '22 01:11

SoroushA


You should explicitly provide an OkHttp instance to Retrofit, otherwise Retrofit would implicitly perform .client(new OkHttpClient()), thus you'd not get a lot of smart things, e.g. disk caching, connection pooling.

See how Jake Wharton clarifies the case in "Making Retrofit Work For You" talk.

Edit

Above mentioned case makes sense if you have multiple retrofit instances.

like image 26
azizbekian Avatar answered Nov 09 '22 23:11

azizbekian