To query, describe, create, or update data from a Salesforce org, Mobile SDK apps call Salesforce REST APIs. Salesforce REST APIs honor SOQL and SOSL strings and can accept and return data in either JSON or XML format. Mobile SDK wraps standard Salesforce REST requests in methods that handle the low-level HTTP configuration for you. For other Salesforce APIs, Mobile SDK provides methods for manually creating a custom request object and receiving the response. You can even use Mobile SDK REST API methods to make unauthenticated and external API calls.
Salesforce supports an ever-growing variety of REST APIs. For an overview of our offerings, see Which API Do I Use? in Salesforce Help. For information on standard REST APIs, see REST API Developer Guide.
Coding REST Interactions
With Android native apps, you do minimal coding to access Salesforce data through REST calls. The classes in the com.salesforce.androidsdk.rest package initialize the communication channels and encapsulate low-level HTTP plumbing. These classes, all of which are implemented by Mobile SDK, include:
ClientManager—Serves as a factory for RestClient instances. It also handles account logins and handshakes with the Salesforce server.
RestClient—Handles protocol for sending REST API requests to the Salesforce server.
Don’t directly create instances of RestClient. Instead, call the ClientManager.getRestClient() method.
RestRequest—Represents REST API requests formatted from the data you provide. Also serves as a factory for instances of itself.
Don’t directly create instances of RestRequest. Instead, call an appropriate RestRequest static getter function such as RestRequest.getRequestForCreate().
Important
RestResponse—Contains the response content in the requested format. The RestRequest class creates RestResponse instances and returns them to your app through your implementation of the RestClient.AsyncRequestCallback interface.
Here’s the basic procedure for using the REST classes on a UI thread:
Create an instance of ClientManager.
Use the SalesforceSDKManager.getInstance().getAccountType() method to obtain the value to pass as the second argument of the ClientManager constructor.
For the LoginOptions parameter of the ClientManager constructor, call SalesforceSDKManager.getInstance().getLoginOptions().
Implement the ClientManager.RestClientCallback interface.
Call ClientManager.getRestClient() to obtain a RestClient instance, passing it an instance of your RestClientCallback implementation. The following code implements and instantiates RestClientCallback inline.
Kotlin
1val accountType = SalesforceSDKManager.getInstance().accountType23val loginOptions = SalesforceSDKManager.getInstance().loginOptions4// Get a rest client5ClientManager(this, accountType, loginOptions,6 SalesforceSDKManager.getInstance().shouldLogoutWhenTokenRevoked()).7 getRestClient(this, object : RestClientCallback(){8 fun authenticatedRestClient(client: RestClient?){9 if(client == null){10 SalesforceSDKManager.getInstance().logout(this@MainActivity)11 return12}13 // Cache the returned client14 this@MainActivity.client = client15}16}17)
Java
1String accountType =2 SalesforceSDKManager.getInstance().getAccountType();34LoginOptions loginOptions =5 SalesforceSDKManager.getInstance().getLoginOptions();6// Get a rest client7new ClientManager(this, accountType, loginOptions,8 SalesforceSDKManager.getInstance().9 shouldLogoutWhenTokenRevoked()).10 getRestClient(this, new RestClientCallback(){11 @Override12 public void13 authenticatedRestClient(RestClient client){14 if(client == null){15 SalesforceSDKManager.getInstance().16 logout(MyActivity.this);17 return;18}19 // Cache the returned client20 MyActivity.this.client = client;21}22}23);
Call a static RestRequest() getter method to obtain the appropriate RestRequest object for your needs. For example, to get a description of a Salesforce object:
Pass the RestRequest object you obtained in the previous step to RestClient.sendAsync() or RestClient.sendSync(). If you’re on a UI thread and therefore calling sendAsync():
Implement the ClientManager.AsyncRequestCallback interface.
Pass an instance of your implementation to the sendAsync() method.
Receive the formatted response through your ASyncRequestCallback.onSuccess() method. Before using the response, double-check that it’s valid by calling RestResponse.isSuccess().
The following code implements and instantiates AsyncRequestCallback inline.
Kotlin
1private fun sendFromUIThread(restRequest: RestRequest){2 client.sendAsync(restRequest, object : AsyncRequestCallback {3 private val start = System.nanoTime()4 override fun onSuccess(request: RestRequest, result: RestResponse){5 // Consume before going back to main thread6 // Not required if you don't do main (UI) thread tasks here7 result.consumeQuietly()8 runOnUiThread {9 // Network component doesn’t report app layer status.10 // Use the Mobile SDK RestResponse.isSuccess() method to check11 // whether the REST request itself succeeded.12 if(result.isSuccess){13 try{14 // Do something with the result15}catch(e: Exception){16 printException(e)17}1819 EventsObservable.get().notifyEvent(EventType.RenditionComplete)20}21}22}2324 override fun onError(exception: Exception){25 printException(exception)26 EventsObservable.get().notifyEvent(EventType.RenditionComplete)27}28})29}
Java
1private void sendFromUIThread(RestRequest restRequest){2 client.sendAsync(restRequest, new AsyncRequestCallback(){3 private long start = System.nanoTime();4 @Override5 public void onSuccess(RestRequest request, final RestResponse result){6 // Consume before going back to main thread7 // Not required if you don't do main (UI) thread tasks here8 result.consumeQuietly();9 runOnUiThread(new Runnable(){10 @Override11 public void run(){12 // Network component doesn’t report app layer status.13 // Use the Mobile SDK RestResponse.isSuccess() method to check14 // whether the REST request itself succeeded.15 if(result.isSuccess()){16 try{17 // Do something with the result18}19 catch(Exception e){20 printException(e);21}22 EventsObservable.get().notifyEvent(EventType.RenditionComplete);23}24}25});26}27 @Override28 public void onError(Exception exception)29{30 printException(exception);31 EventsObservable.get().notifyEvent(EventType.RenditionComplete);32}33});34}
If you’re calling the sendSync() method from a service, use the same procedure with the following changes.
To obtain a RestClient instance call ClientManager.peekRestClient() instead of ClientManager.getRestClient().
Retrieve your formatted REST response from the sendSync() method’s return value.
Checking REST Response Status
A REST response arriving at your app’s onSuccess() callback method indicates only that the network call didn’t fail. This high-level status doesn’t factor in app-level success or failure.
In Mobile SDK for Android, the RestResponse object wraps the underlying okHttp3.Response. To help you code more defensively, RestResponse provides the following convenience methods for inspecting response details.- public isSuccess()
public static boolean isSuccess(int statusCode)
Returns true if the HTTP response status code or the given code is between 200 and 299, indicating app-level success.
public int getStatusCode()
Returns the response status code.
public String getContentType()
Returns the content-type header, if found.
public Map<String, List<String>> getAllHeaders()
Returns all headers associated with this response.
public Response getRawResponse()
Returns the underlying okHttp3.Response object.
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.