Fork me on GitHub

OkHttp's configuration list

OkHttp is the most useful Http library in Android develop,it will help us build a http request and send it to server, bring us responses from server, its main work mechanism is Interceptor Chain(bottom of page).

But in the process of http request, OkHttp read OkHttpClient’s configuration list and add it to transact process. There are main member attributes in OkHttpClient.

main member in OkHttpClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
internal var dispatcher: Dispatcher = Dispatcher()
internal var protocols: List<Protocol> = DEFAULT_PROTOCOLS
internal var connectionSpecs: List<ConnectionSpec> = DEFAULT_CONNECTION_SPECS
internal val interceptors: MutableList<Interceptor> = mutableListOf()
internal val networkInterceptors: MutableList<Interceptor> = mutableListOf()
internal var cookieJar: CookieJar = CookieJar.NO_COOKIES
internal var cache: Cache? = null
internal var hostnameVerifier: HostnameVerifier = OkHostnameVerifier
internal var certificatePinner: CertificatePinner = CertificatePinner.DEFAULT
internal var authenticator: Authenticator = Authenticator.NONE
internal var followRedirects = true
internal var followSslRedirects = true
internal var retryOnConnectionFailure = true
internal var connectTimeout = 10_000
internal var readTimeout = 10_000
internal var writeTimeout = 10_000

And their main responsibilities as follows:

dispatcher

Dispatcher will switch thread when we send a request, as we know, it will make ANR(Android Not Responding) if we do network request in UI thread.

protocols

Protocols that OkHttpClient supports, default include Http/1.1 and Http/2.

1
internal val DEFAULT_PROTOCOLS = immutableListOf(HTTP_2, HTTP_1_1)

connectionSpecs

Socket’s settings OkHttpClient supports, it default include modern TSL(Https) and clear text(http).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
internal val DEFAULT_CONNECTION_SPECS = immutableListOf(
ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT)
```

## interceptors

Interceptors before real interaction with serve, we can add it by ourself it will work before cache interceptor, can see details at Interceptor Chain(bottom of page).

## networkInterceptors

NetworkInterceptors after interacting with serve, we also need add it by ourself, but it may not work if data is cached, can see details at Interceptor Chain(bottom of page).


## cookieJar

CookieJar used to save cookies in our application, but it do not have default implement, so if we need save cookies, we should implement it by ourself in OkHttpClient.

```kotlin
val client = OkHttpClient.Builder().cookieJar(object : CookieJar{
override fun loadForRequest(url: HttpUrl): List<Cookie> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}).build()
```

## cache

Similar with cookJar, it also have default implement, we can implement it by ourself.We should define cache file and maxSize.

```kotlin
/** Create a cache of at most [maxSize] bytes in [directory]. */
constructor(directory: File, maxSize: Long) : this(directory, maxSize, FileSystem.SYSTEM)

hostnameVerifier

HostnameVerifier used to verify whether hostname is valid.

certificatePinner

CertificatePinner used to verify certificate, it has default implement, but if we need to self certificate, we can add it by ourself, example as follows:

1
2
3
val client = OkHttpClient.Builder()
.certificatePinner(CertificatePinner.Builder().add("XXXXXX").add("XXXXXX").build())
.build()

authenticator

Authenticator used to add authenticator header after received service response code 401(authorize fail). Examples as follows

1
2
3
4
5
6
val client = OkHttpClient.Builder().authenticator(object : Authenticator{
override fun authenticate(route: Route?, response: Response): Request? {
val request = Request.Builder().addHeader("Authorization", " Bearer <XXXXXXX>")
return request.build()
}
}).build()

followRedirects

FollowRedirects can set whether follow url when received response 3XX(redirect).

followSslRedirects

FollowSslRedirects can set whether follow url when received response 3XX(redirect) and current url is a http link, follow url is a https link.

connectTimeout

Set connect socket time limit.

readTimeout

Set read socket data time limit.

writeTimeout

Set write time limit in write data to socket.

other

Can see https’s connection build process at Https build process and OkHttp’s interceptors(bottom of page) chain at Interceptor Chain(bottom of page).