Query web services
United States
Canada
Europe
Latin America
Clover Android SDK examples
Clover Android SDK examples use the Clover APIs to connect to Services, query Clover ContentProviders
, and fetch data directly from the Clover REST API.
To see our APIs in action, visit our Clover Android SDK examples directory on GitHub. To authenticate with Clover Web services, see the CloverAuth.java example.
Query a web service
WebServiceActivity
shows how the Clover REST API can be queried directly once the Clover Account
and authToken
are retrieved.
- Get a merchant's
CloverAccount
:
private Account account;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
account = CloverAccount.getAccount(this);
- Get your app's
authToken
using theCloverAuth.authenticate
call in a background thread:
private void getCloverAuth() {
new AsyncTask<Void, Void, CloverAuth.AuthResult>() {
@Override
protected CloverAuth.AuthResult doInBackground(Void... params) {
try {
Context context = getApplicationContext();
return CloverAuth.authenticate(context);
} catch (Exception e) {
Log.e(TAG, "Error authenticating", e);
}
return null;
}
@Override
protected void onPostExecute(CloverAuth.AuthResult result) {
cloverAuth = result;
if (cloverAuth != null && cloverAuth.authToken !=null) {
mToken.setText("Token: " + cloverAuth.authToken);
} else {
mToken.setText("Error retrieving token."));
}
}
}.execute();
}
}
A successful authentication attempt returns an authResult
containining the authToken
and baseUrl
.
- Query the REST API to get merchant information using an HTTP client of your choice.
// First, get the merchant ID and base URL
// and use them to construct the request URL
String merchantId = cloverAuth.merchantId;
String merchantUri = "/v3/merchants/" + merchantId;
String url = cloverAuth.baseUrl + merchantUri;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", "Bearer " + cloverAuth.authToken)
.build();
// Second, get merchant information using the request URL
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException(String.valueOf(response));
System.out.println(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
Updated 24 days ago