Android SDK
Developers need to have basic GraphQL capabilities before accessing the ArcBlock Android SDK. We also provide a fully functional ArcBlock OCAP Playground where developers can use it to write and test the GraphQL statements they want.
Absdkcorekit Library
Absdkcorekit Library is on the basis of the apollo-android encapsulated Data
layer core library, we introduced LifecycleObserver
makes the SDK can sense the page life cycle, To do memory optimization in the SDK layer, developers only need to pass in a LifecycleOwner
object when they are using it. (both the Fragment
and AppCompatActivity
in the support library have implemented the LifecycleOwner
interface, which can be used directly, otherwise, they can realize LifecycleOwner
by referring to the implementation in the support library above.)
1. Import Absdkcorekit Library
Add the following code to the project root directory build.gradle
file:
buildscript {
dependencies {
//...
classpath 'com.apollographql.apollo:apollo-gradle-plugin:1.0.0-alpha3'
}
}
allprojects {
repositories {
//...
maven { url "http://android-docs.arcblock.io/release" }
}
}
2
3
4
5
6
7
8
9
10
11
12
13
Add the following code into the app module build.gradle
file:
apply plugin: 'com.apollographql.android'
apollo {
useJavaBeansSemanticNaming = true
}
//......
dependencies {
implementation 'com.arcblock.corekit:absdkcorekit:0.3.5'
}
2
3
4
5
6
7
8
9
10
schema.json
and create .graphql
file
2. Create graphql directory and download add Recommend to create a directory which is the same as app module package name, such as sample code of
arcblock-android-sdk/app/src/main/graphql/com/arcblock/sdk/demo/
,arcblock-android-sdk/app/src/main/graphql/
behind relatively catalogue and sample project package name is consistent, Of course, you can also specify directory correlation, please refer to the explicit-schema-location .
schema.json
download address: bitcoin.json, ethereum.json download later and all need to be renamed asschema.json
, you can be in the sample project ofarcblock-android-sdk/app/src/main/graphql/com/arcblock/sdk/demo/btc/
orarcblock-android-sdk/app/src/main/graphql/com/arcblock/sdk/demo/eth/
directory to find this file, you can directly copy to use.- Using ArcBlock OCAP Playground write and test by GraphQL statements, and make a copy of it to a
.graphql
file, you can be in the sample project ofarcblock-android-sdk/app/src/main/graphql/com/arcblock/sdk/demo/btc/
orarcblock-android-sdk/app/src/main/graphql/com/arcblock/sdk/demo/eth/
directory to find similar sample files. - Build your project, after successful compilation, will you be in
build
found directory compiled automatically generatedJava
code, you can be in the sample project ofarcblock-android-sdk/app/build/generated/source/apollo/
directory to see the generated code, you don't need to modify the automatically generated code.
3. Implement common query function
New one
CoreKitQuery
object:CoreKitQuery coreKitQuery = new CoreKitQuery(this, DemoApplication.getInstance().abCoreKitClientBtc());
1To initiate a query request, you only need to set the corresponding query object and callback object in the method, and the results of the query can be obtained in the callback object:
coreKitQuery.query(AccountByAddressQuery.builder().address(address).build(), new CoreKitResultListener<AccountByAddressQuery.Data>() { @Override public void onSuccess(AccountByAddressQuery.Data data) { // get the data } @Override public void onError(String errMsg) { // get the error message } @Override public void onComplete() { // query complete } });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WARNING
A CoreKitQuery object can be used for processing multiple query objects.
4. Implement paged Query function
New
PagedQueryHelper
object, which is used to build the initial (or refresh) query object used for paging queries and to add more query objects, as well as map processing of the data, sets the paged flag:mPagedQueryHelper = new PagedQueryHelper<BlocksByHeightQuery.Data, BlocksByHeightQuery.Datum>() { @Override public Query getInitialQuery() { return BlocksByHeightQuery.builder().fromHeight(startIndex).toHeight(endIndex).build(); } @Override public Query getLoadMoreQuery() { PageInput pageInput = null; if (!TextUtils.isEmpty(getCursor())) { pageInput = PageInput.builder().cursor(getCursor()).build(); } return BlocksByHeightQuery.builder().fromHeight(startIndex).toHeight(endIndex).paging(pageInput).build(); } @Override public List<BlocksByHeightQuery.Datum> map(BlocksByHeightQuery.Data data) { if (data.getBlocksByHeight() != null) { // set page info to PagedQueryHelper if (data.getBlocksByHeight().getPage() != null) { // set is have next flag to PagedQueryHelper setHasMore(data.getBlocksByHeight().getPage().isNext()); // set new cursor to PagedQueryHelper setCursor(data.getBlocksByHeight().getPage().getCursor()); } return data.getBlocksByHeight().getData(); } return null; } };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30New a
CoreKitPagedQuery
object, introduced into the abovePagedQueryHelper
objects:mCoreKitPagedQuery = new CoreKitPagedQuery(this, DemoApplication.getInstance().abCoreKitClientBtc(), mPagedQueryHelper);
1Set the paging query data processing callback and launch the initial page query:
mCoreKitPagedQuery.setPagedQueryResultListener(new CoreKitPagedQueryResultListener<BlocksByHeightQuery.Datum>() { @Override public void onSuccess(List<BlocksByHeightQuery.Datum> datas) { // Processing the data that comes back from paging, the total data will be returned here, please refer to the demo code for details } @Override public void onError(String errMsg) { // get the error message } @Override public void onComplete() { // query complete } }); // start initial query mCoreKitPagedQuery.startInitQuery();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18Page refresh query:
mCoreKitPagedQuery.startInitQuery();
1Load next page query:
mCoreKitPagedQuery.startLoadMoreQuery();
1
WARNING
Different from CoreKitQuery
, a CoreKitPagedQuery
object can only be in the service of a particular paging query.
5. Implement mutation
New a
CoreKitMutation
object:CoreKitMutation coreKitMutation = new CoreKitMutation(this, DemoApplication.getInstance().abCoreKitClient());
1To initiate mutation, you only need to set the corresponding mutation object and callback object in the method. The results of mutation can be obtained in the callback object:
coreKitMutation.mutation(mutation, new CoreKitResultListener<XXMutation.Data>() { @Override public void onSuccess(XXMutation.Data data) { // get the data } @Override public void onError(String errMsg) { // get the error message } @Override public void onComplete() { // mutation complete } });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WARNING
A CoreKitMutation object can be used for processing more than one mutation object.
6. Implement data subscription function
Open the socket switch when init the ABCoreClient :
ABCoreKitClient.xxx .xxxx .setOpenSocket(true) // the socket switch .xxxx .build();
1
2
3
4
5New a
CoreKitSubscription
object:mCoreKitSubscription = new CoreKitSubscription<>(this, DemoApplication.getInstance().abCoreKitClientEth(), new NewBlockMinedSubscription(), NewBlockMinedSubscription.Data.class);
1Set ResultListener:
mCoreKitSubscription.setResultListener(new CoreKitSubscriptionResultListener<NewBlockMinedSubscription.Data>() { @Override public void onSuccess(NewBlockMinedSubscription.Data data) { // get the new data } @Override public void onError(String errMsg) { // get the error message } });
1
2
3
4
5
6
7
8
9
10
11Set CoreKitSocketStatusCallBack:
mCoreKitSubscription.setCoreKitSocketStatusCallBack(new CoreKitSocketStatusCallBack() { @Override public void onOpen() { // do something here when socket on open } @Override public void onClose() { // do something here when socket on close } @Override public void onError() { // do something here when on error } });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WARNING
A CoreKitSubscription object can only serve a specific subscription object.
7. Support HMAC Authentication
Go https://console.arcblock.io registered an account, and in
Settings -> Security Settings
to create a set ofAccess Key
andAccess Secret
.Create a
ABCoreKitClient
and open the HMAC Authentication switch:mABCoreClientBtcWithHMAC = ABCoreKitClient.builder(this, CoreKitConfig.ApiType.API_TYPE_BTC) .setOpenOkHttpLog(true) .setEnableHMAC(true) // open HMAC Authentication .setDefaultResponseFetcher(ApolloResponseFetchers.NETWORK_ONLY) .build();
1
2
3
4
5In app module
AndroidManifest.xml
configured in the application ofAccess Key
andAccess Secret
:<!-- For ArcBlock SDK--> <meta-data android:name="ArcBlock_Access_Key" android:value="<Your Access Key>" /> <meta-data android:name="ArcBlock_Access_Secret" android:value="<Your Access Secret>" />
1
2
3
4
5
6
7
8. Other Settings
CustomType
Setting:First, add
customTypeMapping
in thebuild.gradle
file ofapp module
:apollo { customTypeMapping = [ "Date" : "java.util.Date" ] }
1
2
3
4
5Create the corresponding
CustomTypeAdapter
used to resolve the correspondingCustomType
:CustomTypeAdapter dateCustomTypeAdapter = new CustomTypeAdapter<Date>() { @Override public Date decode(CustomTypeValue value) { try { SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000000'Z'"); utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date gpsUTCDate = utcFormat.parse(value.value.toString()); return gpsUTCDate; } catch (ParseException e) { e.printStackTrace(); } return null; } @Override public CustomTypeValue encode(Date value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000000'Z'"); return new CustomTypeValue.GraphQLString(sdf.format(value)); } };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ABCoreKitClient
initialization:Recommended in the main process of
Application onCreate
method initializes a global singletonABCoreKitClient
object:mABCoreClientBtc = ABCoreKitClient.builder(this, CoreKitConfig.ApiType.API_TYPE_BTC) .addCustomTypeAdapter(CustomType.DATETIME, dateCustomTypeAdapter) .setOpenOkHttpLog(true) .setDefaultResponseFetcher(ApolloResponseFetchers.CACHE_FIRST) .build();
1
2
3
4
5At the time of initialization, you can pass in custom
okHttpClient
,CustomTypeAdapter
,ResponseFetcher
parameters.
License
ArcBlockSDK is available under the MIT license. See the LICENSE file for more info.