Managing the Request Queue
The RestClient class internally uses an instance of the OkHttpClient class to manage REST API requests. You can access underlying OkHttp objects directly to cancel pending requests. To manage a specific request, you can use the OkHttp Call object returned by the RestClient.sendAsync() Mobile SDK method.
Example
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/ .
See Also
Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.