Overview

GeeTest captcha Android SDK supports for native Android.

Development environment

Item Description
Target Android 4.0+
Development environment Android Studio 2.1.3
Compile tool gradle (for ant compile, extract SDK, jar package and resource file access)
System dependency None
Third-party dependency OKHTTP
Dependency in demo OKHTTP

Resources

Item Description
Data communication flow chart Data communication flow chart , Interaction process
SDK API reference Check gt3-android-docs or the comments in examples

Custom API documentation |Document download
Error code |Error code

Installation

Download SDK

Remote dependency acquisition

The code is hosted in jcenter. Check if the jcenter() configuration is declared in the main project build.gradle file before using it

# Compatible androidx
implementation 'com.geetest.sensebot:sensebot:4.2.3'

Manual download fetch

Please click the link below to download the latest SDK (in .zip format) (Compatible androidx).

gt3-android-sdk

Import SDK(If there is a demand on the Google Store, please use the manual download integration method)

Except using the remote repository, you can also drag and drop the .aar file into libs folder.

import

After adding the .aar file into the libs folder, please check if .aar has been successfully imported into Library. Please insert the following code into the build.gradle of your project.

repositories {
flatDir {
dirs 'libs'
}
}

Then, add dependency for .aar package manually (AAR does not pass third-party dependencies by default, you need to manually add!).

compile(name: 'geetest_sensebot_android_vx.y.z_date', ext: 'aar')
implementation 'com.squareup.okhttp3:okhttp:{latestVersion}'

libraries

Configure API

Android sdk provide the following APIs.

  1. captcha initialization
  2. Start captcha verification
  3. Get the verification callback data

Please firstly integrate server SDK, configure the id and key (get from GeeTest dashboard), then implement API1 and API2 in the captcha initialization method.

Please refer to the following code example for integration.

Compile and run your project

Compile your project and test GeeTest captcha.

build

Two integration options

  1. Bind GeeTest captcha to a custom button

    sample1

  2. Integrate with GeeTest captcha button

    sample2

Code example

File configuration

  1. Set permission

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  2. Proguard rules

    # GeeTest SDK has already been obfuscated, please do not obfuscate it again  
    -dontwarn com.geetest.sdk.**
    -keep class com.geetest.sdk.**{*;}

Bind GeeTest captcha to a custom button

  1. New a captcha object

    Please new the object within the lifecycle of oncreate

    // Please initialize it in the oncreate method to get enough gesture data to ensure a successful first round of validation
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  1. Configure parameters for captcha initialization

    Configure it in the life cycle of oncreate or configure it when it has been called

    // 配置bean文件,也可在oncreate初始化
    gt3ConfigBean = new GT3ConfigBean();
    // 设置验证模式,1:bind,2:unbind
    gt3ConfigBean.setPattern(1);
    // 设置点击灰色区域是否消失,默认不消息
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // 设置语言,如果为null则使用系统默认语言
    gt3ConfigBean.setLang(null);
    // 设置加载webview超时时间,单位毫秒,默认10000,仅且webview加载静态文件超时,不包括之前的http请求
    gt3ConfigBean.setTimeout(10000);
    // 设置webview请求超时(用户点选或滑动完成,前端请求后端接口),单位毫秒,默认10000
    gt3ConfigBean.setWebviewTimeout(10000);
    // 设置回调监听
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // 开启验证
    gt3GeetestUtils.startCustomFlow();

Integrate with GeeTest captcha button

  1. Insert captcha button

    <com.geetest.sdk.views.GT3GeetestButton
    android:id="@+id/btn_geetest"
    android:layout_width="290dp"
    android:layout_height="44dp"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="horizontal" />
  2. New a captcha object

    Please new the object in the life cycle of oncreate

    // 务必在oncreate方法里配置
    GT3GeetestButton geetestButton = (GT3GeetestButton) findViewById(R.id.btn_geetest);
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  3. Configure parameter for captcha initialization

    Configure it in the life cycle of oncreate or configure it when it has been called

    // 配置bean文件,也可在oncreate初始化
    gt3ConfigBean = new GT3ConfigBean();
    // 设置验证模式,1:bind,2:unbind
    gt3ConfigBean.setPattern(2);
    // 设置点击灰色区域是否消失,默认不消息
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // 设置语言,参考API文档。如果为null则使用系统默认语言(TODO 若想设置语言立即生效,则init需要在setContentView之前)
    gt3ConfigBean.setLang(null);
    // 设置加载webview超时时间,单位毫秒,默认10000,仅且webview加载静态文件超时,不包括之前的http请求
    gt3ConfigBean.setTimeout(10000);
    // 设置webview请求超时(用户点选或滑动完成,前端请求后端接口),单位毫秒,默认10000
    gt3ConfigBean.setWebviewTimeout(10000);
    // 设置回调监听
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // 绑定
    geetestButton.setGeetestUtils(gt3GeetestUtils);

Handle captcha callback

To deal with the callback data during the verification, you need to process API1 request via onButtonClick method, and process API2 request via onDialogResult method.

	
gt3ConfigBean.setListener(new GT3Listener() {

/**
* 验证码加载完成
* @param duration 加载时间和版本等信息,为json格式
*/
@Override
public void onDialogReady(String duration) {
Log.e(TAG, "GT3BaseListener-->onDialogReady-->" + duration);
}

/**
* 图形验证结果回调
* @param code 1为正常 0为失败
*/
@Override
public void onReceiveCaptchaCode(int code) {
Log.e(TAG, "GT3BaseListener-->onReceiveCaptchaCode-->" + code);
}

/**
* 自定义api2回调
* @param result,api2请求上传参数
*/
@Override
public void onDialogResult(String result) {
Log.e(TAG, "GT3BaseListener-->onDialogResult-->" + result);
// 开启api2逻辑
new RequestAPI2().execute(result);
}

/**
* 统计信息,参考接入文档
* @param result
*/
@Override
public void onStatistics(String result) {
Log.e(TAG, "GT3BaseListener-->onStatistics-->" + result);
}

/**
* 验证码被关闭
* @param num 1 点击验证码的关闭按钮来关闭验证码, 2 点击屏幕关闭验证码, 3 点击返回键关闭验证码
*/
@Override
public void onClosed(int num) {
Log.e(TAG, "GT3BaseListener-->onClosed-->" + num);
}

/**
* 验证成功回调
* @param result
*/
@Override
public void onSuccess(String result) {
Log.e(TAG, "GT3BaseListener-->onSuccess-->" + result);
}

/**
* 验证失败回调
* @param errorBean 版本号,错误码,错误描述等信息
*/
@Override
public void onFailed(GT3ErrorBean errorBean) {
Log.e(TAG, "GT3BaseListener-->onFailed-->" + errorBean.toString());
}

/**
* 自定义api1回调
*/
@Override
public void onButtonClick() {
new RequestAPI1().execute();
}

});

Please refer to the demo project for detailed examples.