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

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/.

See Also

We've Moved

Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.