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.
The following examples show how to perform some common network operations with OkHttpClient.
Common Imports
1 import okhttp3.Headers;
2 import okhttp3.HttpUrl;
3 import okhttp3.OkHttpClient;
4 import okhttp3.Call;
5 import okhttp3.Dispatcher;
6 import okhttp3.Request;
7 import okhttp3.RequestBody;
8 import okhttp3.Response;
Obtain the Current OkHttp Client Handle
To get the handle of the OkHttpClient that the current RestClient instance is using:
Kotlin
1 var okClient = restClient . getOkHttpClient ( )
Java
1 OkHttpClient okClient = restClient . getOkHttpClient ( ) ;
Obtain the OkHttp Dispatcher
Kotlin
1 var dispatcher = restClient . getOkHttpClient ( ) . dispatcher ( )
Java
1 Dispatcher dispatcher = restClient . getOkHttpClient ( ) . dispatcher ( ) ;
Cancel All Pending Calls
Kotlin
1 var dispatcher = restClient . getOkHttpClient ( ) . dispatcher ( )
2 dispatcher . cancelAll ( )
Java
1 Dispatcher dispatcher = restClient . getOkHttpClient ( ) . dispatcher ( ) ;
2 dispatcher . cancelAll ( ) ;
Store the OkHttp Handle to a REST Request
Kotlin
1 var call = restClient . sendAsync ( restRequest, callback )
Java
1 Call call = restClient . sendAsync ( restRequest, callback ) ;
Cancel a Specific REST Request Using a Stored Handle
Kotlin
1 var call = restClient . sendAsync ( restRequest, callback )
Java
1 Call call = restClient . sendAsync ( restRequest, callback ) ;
2 ...
3 call . cancel ( ) ;
For more information, see square.github.io/okhttp/ .
Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.