OkHttp: The Underlying Network Library
Beginning with Mobile SDK 4.2, the Android REST request system
uses OkHttp (v3.2.0), an open-source external library from Square Open Source, as its underlying
architecture. This library replaces the Google Volley library from past releases. As a result,
Mobile SDK no longer defines the WrappedRestRequest
class.
Example
The following examples show how to perform some common network
operations with OkHttpClient.
- Common Imports
-
1import okhttp3.Headers; 2import okhttp3.HttpUrl; 3import okhttp3.OkHttpClient; 4import okhttp3.Call; 5import okhttp3.Dispatcher; 6import okhttp3.Request; 7import okhttp3.RequestBody; 8import okhttp3.Response; - Obtain the Current OkHttp Client Handle
- To get the handle of the OkHttpClient that the
current RestClient instance is using:
- Kotlin
-
1var okClient = restClient.getOkHttpClient() - Java
-
1OkHttpClient okClient = restClient.getOkHttpClient();
- Obtain the OkHttp Dispatcher
-
- Kotlin
-
1var dispatcher = restClient.getOkHttpClient().dispatcher() - Java
-
1Dispatcher dispatcher = restClient.getOkHttpClient().dispatcher();
- Cancel All Pending Calls
-
- Kotlin
-
1var dispatcher = restClient.getOkHttpClient().dispatcher() 2dispatcher.cancelAll() - Java
-
1Dispatcher dispatcher = restClient.getOkHttpClient().dispatcher(); 2 dispatcher.cancelAll();
- Store the OkHttp Handle to a REST Request
-
- Kotlin
-
1var call = restClient.sendAsync(restRequest, callback) - Java
-
1Call call = restClient.sendAsync(restRequest, callback);
- Cancel a Specific REST Request Using a Stored Handle
-
- Kotlin
-
1var call = restClient.sendAsync(restRequest, callback) - Java
-
1Call call = restClient.sendAsync(restRequest, callback); 2 ... 3 call.cancel();
For more information, see square.github.io/okhttp/.