Platform Docs

Integrating with Clover Android SDK

Integration with Clover Android SDK

Use the Clover Android SDK and related Java documentation to access merchant data. For this example, we will display information on one inventory item.

NOTE

For this tutorial, you must have one item in your merchant's inventory. You can do this through the Inventory app.

  1. Add a dependency for the latest version of the Clover Android SDK to the gradle.build file for your app's module. Adding this dependency with the following code will always pull the latest released version of our Clover SDK.
dependencies { 
compile 'com.clover.sdk:clover-android-sdk:latest.release'
}

Your gradle.build file should look like the file in the image below.

  1. Add <uses-permission android:name="android.permission.GET_ACCOUNTS"/> to the AndroidManifest (this is required to connect with the merchant account).
  1. Add an ID to the textView element to enable dynamic text insertion.
  1. Finally, you can replace your Activity with this code to pull out the merchant data:
package com.your.package.name.here;

import android.accounts.Account;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.v1.BindingException;
import com.clover.sdk.v1.ClientException;
import com.clover.sdk.v1.ServiceException;
import com.clover.sdk.v3.inventory.InventoryConnector;
import com.clover.sdk.v3.inventory.Item;


public class MainActivity extends AppCompatActivity {

    private Account mAccount;
    private InventoryConnector mInventoryConnector;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mTextView = (TextView) findViewById(R.id.activity_main_text_view);

        // Retrieve the Clover account
        if (mAccount == null) {
            mAccount = CloverAccount.getAccount(this);

            if (mAccount == null) {
                return;
            }
        }

        // Connect InventoryConnector
        connect();

        // Get Item
        new InventoryAsyncTask().execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnect();
    }

    private void connect() {
        disconnect();
        if (mAccount != null) {
            mInventoryConnector = new InventoryConnector(this, mAccount, null);
            mInventoryConnector.connect();
        }
    }

    private void disconnect() {
        if (mInventoryConnector != null) {
            mInventoryConnector.disconnect();
            mInventoryConnector = null;
        }
    }

    private class InventoryAsyncTask extends AsyncTask<Void, Void, Item> {

        @Override
        protected final Item doInBackground(Void... params) {
            try {
                //Get inventory item
                return mInventoryConnector.getItems().get(0);

            } catch (RemoteException | ClientException | ServiceException | BindingException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected final void onPostExecute(Item item) {
            if (item != null) {
                mTextView.setText(item.getName());
            }
        }
    }
}

Now you can sideload your app onto your device to see it in action.


Integrating with Clover Android SDK


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.