Bootstrap an Android APP

Before starting to work, let's first look at the final implementation effect. We launched the query by calling the API of Android SDK, and got the basic details of the account by entering the address of BTC account: address, balance, total number of sended and total number of received.

Demo Link

quickstart

1. Create project and config

First, open Android Studio and create a new project, after which you need to configure the following.

1 Configure build.gradle under the project folder

This step mainly configures the global maven repository address and the necessary apollo-gradle-plugin dependencies. The detailed code is as follows:

buildscript {
  dependencies {
    classpath 'com.apollographql.apollo:apollo-gradle-plugin:1.0.0-alpha3'
  }
}

allprojects {
   repositories {
	maven { url "http://android-docs.arcblock.io/release" }
   }
}
1
2
3
4
5
6
7
8
9
10
11

2 Configure build.gradle under the module folder

In this step, we need to add SDK dependencies for module and some configuration related to apollo-android. The detailed code is as follows:

apply plugin: 'com.apollographql.android'

apollo {
    useJavaBeansSemanticNaming = true
    // for Btc custom type
    customTypeMapping = [
        "Date" : "java.util.Date",
        "Long" : "java.lang.Long",
    ]
}

dependencies {
  implementation 'com.arcblock.corekit:absdkcorekit:0.3.5'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

2. Graphql files and schema.json

Recommended to save graphql files and schema.json under {moduleName}/src/main/{modulePagekageName}/, for example, we put them in a quickstart/src/main/com/arcblock/sdk/quickstart/ below:

schema.json download address: bitcoin.json, renamed after download and save to the directory which was defined above .

The rapid integration of the function of the demo project is just the OCAP BTC, actual use process, if use the ETH related functions still need to download to save the ETH schema.json, you may refer to specific arcblock-android-sdk app module.

And graphql files can be first code in OCAP Playground, and then the experiment after successful replication is named graphql save file to the directory above, the graphql query is as follows:

query accountByAddress($address: String!){
  accountByAddress(address:$address) {
    address
    balance
    numberTxsSent
    numberTxsReceived
    totalAmountSent
    totalAmountReceived
  }
}
1
2
3
4
5
6
7
8
9
10

3. Call SDK API to query

After the preparation work is finished, we can make a simple drawing of the page. After drawing, we can get the query result of the graphql file written above and display it on the view by calling the query function of SDK. The core code is as follows:

CoreKitQuery coreKitQuery = new CoreKitQuery(this, ABCoreKitClient.defaultInstance(this, CoreKitConfig.ApiType.API_TYPE_BTC));
coreKitQuery.query(AccountByAddressQuery.builder().address(addressEt.getText().toString()).build(), new CoreKitResultListener<AccountByAddressQuery.Data>() {
    @Override
    public void onSuccess(AccountByAddressQuery.Data data) {
        if (data.getAccountByAddress()!=null) {
            addressTv.setText(data.getAccountByAddress().getAddress());
            balanceTv.setText(BtcValueUtils.formatBtcValue(data.getAccountByAddress().getBalance()));
            totalAmountSentTv.setText(BtcValueUtils.formatBtcValue(data.getAccountByAddress().getTotalAmountSent()));
            totalAmountReceivedTv.setText(BtcValueUtils.formatBtcValue(data.getAccountByAddress().getTotalAmountReceived()));
        } else {
            ToastUtils.showShort("error=>data is null.");
        }
    }

    @Override
    public void onError(Throwable e) {
        ToastUtils.showShort("error=>"+e.toString());
    }

    @Override
    public void onComplete() {

    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

4. Explore more features

The above process mainly illustrates how to integrate the Android SDK and through its API calls OCAP query service, here is only a simple accountByAddress, however, the ability of the OCAP is far more than that:

  • BTC related queries
  • ETH related queries
  • BTC related subscriptions
  • ETH related subscriptions
  • ...

With OCAP Playground can complete these exploration, with the help of arcblock-android-sdk you can will explore the outcome of the work on the android platform.

Last Updated: 11/30/2018, 10:39:27 AM