Querying web services

To see our APIs in action, visit the Clover Android SDK examples directory in the Clover Android SDK. The examples use the Clover APIs to connect to Services, query Clover ContentProviders, and fetch data directly from the Clover REST API.

Querying a Web Service

WebServiceActivity shows how the Clover REST API can be queried directly once the Clover Account and authToken are retrieved.

  1. Get a merchant's CloverAccount:
private Account account;
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  account = CloverAccount.getAccount(this);
  1. Get your app's authToken using the CloverAuth.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.

  1. 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();
}

You can find more examples in Clover's Android Examples Repository on GitHub.

📘

NOTE

There are important changes to the Android SDK auth token generation.


Did this page help you?