Why do developers install OkHttp for Retrofit if Retrofit already uses OkHttp internally

I know that Retrofit uses OkHttp internally. But, I see that some developers have provided an approach like below

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

So, can someone clarify why this is necessary?

I heard that this can be useful for testing when we can use a custom Interceptor while mixing HTTP requests.

+4
source share
3 answers

OkHttp , OkHttpClient. , OkHttp. , JavaDoc. , , , -, DNS, ( ), -, ( HTTPS, HTTP ) .

EDIT: , , API API REST. , , VPN - , , . , , , . Spotify YouTube. WiFi, (, UDP). 3G, TCP .

+4

OkHttp Retrofit, Retrofit .client(new OkHttpClient()), , , . , .

, " " .

Edit

, .

+3

. OkHttp .

OkHttpClient. -, - .

OkHttpClient for the prod product should work as expected. OkHttpClient for artificial taste should use a custom Interceptor. This custom Interceptor can be used to analyze server requests and apply your predefined responses from your resource folder. This is a pretty elegant way to mock a server.

0
source

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


All Articles