API reference

更新时间:
复制 MD 格式

This topic describes the APIs required to integrate the Captcha Android client.

Utility class APIs

Get the AlicomCaptcha4Client utility class object

This is not a singleton implementation. You must save the AlicomCaptcha4Client object obtained through getClient() for use in onDestroy or other lifecycle methods.

public static AlicomCaptcha4Client getClient(Context context)

Parameter

Type

Description

context

Context

The context.

Initialize with default configurations

Calling the init() method loads the Captcha. If you call this method before the onResume() lifecycle method, the Captcha is preloaded. When you execute verifyWithCaptcha() to display the Captcha, the preloaded Captcha is displayed.

public AlicomCaptcha4Client init(String captchaId)

Parameter

Type

Description

captchaId

String

The verification ID. Pass the appId generated after you create a verification solution in the console.

Initialize with custom configurations

public AlicomCaptcha4Client init(String captchaId, AlicomCaptcha4Config config)

Parameter

Type

Description

captchaId

String

The verification ID. Pass the appId generated after you create a verification solution in the console.

config

AlicomCaptcha4Config

The parameter configuration object.

Add a listener object for verification results

Adds a listener object for verification results. The callback is triggered after the Captcha loads successfully and the user solves it.

public AlicomCaptcha AlicomCaptcha4Client
addOnSuccessListener(AlicomCaptcha4Client.OnSuccessListener response)

Parameter

Type

Description

response

AlicomCaptcha4Client.OnSuccessListener

The listener object for verification results.

Add a listener for successful frontend loading

Adds a listener that is called back after the Captcha frontend loads successfully. If preloading is complete, the callback is triggered immediately after you call verifyWithCaptcha.

public AlicomCaptcha4Client
addOnWebViewShowListener(AlicomCaptcha4Client.OnWebViewShowListener listener)

Parameter

Type

Description

listener

AlicomCaptcha4Client.OnWebViewShowListener

The listener object for successful frontend loading.

Add a listener for Captcha loading failures

Adds a listener that is called back if an error occurs while the Captcha is loading.

public AlicomCaptcha AlicomCaptcha4Client
addOnFailureListener(AlicomCaptcha4Client.OnFailureListener listener)

Parameter

Type

Description

listener

AlicomCaptcha4Client.OnFailureListener

The listener object for Captcha loading failures.

Start the verification flow

Starts the verification flow and loads the Captcha. Before you call verifyWithCaptcha(), you must call init(), addOnSuccessListener(), and addOnFailureListener. Otherwise, an error may occur.

public AlicomCaptcha AlicomCaptcha4Client verifyWithCaptcha()

Cancel verification

Cancels the verification and closes the verification window. Before you call verifyWithCaptcha(), you must call init(), addOnSuccessListener(), and addOnFailureListener. Otherwise, an error may occur.

public void cancel()

Enable or disable log printing

public void setLogEnable(boolean enable)

Destroy long-lifecycle resources

Call this method in the onDestroy lifecycle method of the interface where the Captcha is used.

public AlicomCaptcha void destroy()

Get the version number

Returns the version number as a string.

public static String getVersion()

Check if the system WebView is supported

public static Pair<Boolean, String> isSupportWebView(Context context)

Parameter

Type

Description

first

Boolean

Indicates whether WebView is supported. If not, you can implement fallback logic.

second

String

The error description if the WebView object fails to load.

Parameter configuration class APIs

Set the request timeout period

public AlicomCaptcha Builder setTimeOut(int timeOut)

Parameter

Type

Description

timeOut

int

The request timeout period in ms. The default value is 10000.

Set the resource path for WebView loading

By default, the gt4-index*.html file in the assets folder is loaded. You do not need to call this method unless you have specific requirements.

public AlicomCaptcha Builder setResourcePath(String html)

Parameter

Type

Description

html

String

The loading path for WebView.

Control the language for the loaded WebView

Sets the language for JavaScript (JS). This method only controls the language of the loaded WebView. The multilingual feature of the software development kit (SDK) uses the current language of the application. If this parameter is not set or is set to null, the application's default language short code is passed to the frontend.

public AlicomCaptcha Builder setLanguage(String language)

Parameter

Type

Description

language

String

The language short code supported by the frontend.

The supported languages and their short codes are as follows:

Language

Language

Language short code

Simplified Chinese

Chinese(Simplified)

zho

Traditional Chinese (Hong Kong)

Chinese(Hong Kong)

zho-hk

Traditional Chinese (Taiwan)

Chinese(Taiwan)

zho-tw

American English

English

eng

British English

English

eng-gb

Japanese

Japanese

jpn

Indonesia

Indonesian

ind

Korean

Korean

kor

Russian

Russian

rus

Arabic

Arabic

ara

Spanish

Spanish

spa

French

French

fra

German

German

deu

Uyghur

Uyghur

udm

Brazilian Portuguese

Portuguese(Brazil)

pon

European Portuguese

Portuguese(Europe)

por

Set whether the dialog box disappears when the gray area is clicked

public AlicomCaptcha Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside)

Parameter

Type

Description

canceledOnTouchOutside

boolean

Sets whether the dialog box disappears when the gray area is clicked.

  • true (default): The dialog box disappears.

  • false: Remains visible

Set whether to enable debug mode

public AlicomCaptcha Builder setDebug(boolean debug)

Parameter

Type

Description

debug

boolean

Sets whether to enable debug mode.

  • True: Yes

  • false (default): No

Set the WebView background color

Sets the background color of the WebView. This controls the background color of the Android WebView control, which is different from the background color of the frontend JS.

public AlicomCaptcha Builder setBackgroundColor(int backgroundColor)

Parameter

Type

Description

backgroundColor

int

The color resource address or the return value of Color.parseColor().

Set the theme style for the Captcha dialog box

public Builder setDialogStyle(String dialogStyle)

Parameter

Type

Description

dialogStyle

String

The name of the theme style for the dialog box.

Default theme style

<style name="gt4_captcha_dialog_style" parent="android:Theme.Dialog">
<!--No windowFrame for the Dialog-->
<item name="android:windowFrame">@null</item>
<!--Specifies whether the window floats on top of the activity-->
<item name="android:windowIsFloating">true</item>
<!--Specifies whether the window is translucent-->
<item name="android:windowIsTranslucent">true</item>
<!--Removes the title-->
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<!--Specifies whether to use a black translucent background outside the display area-->
<item name="android:backgroundDimEnabled">true</item>
<!--Sets the background of the dialog-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:padding">0dp</item>
</style>

Set the listener callback for when the verification window is displayed

public void setDialogShowListener(AlicomCaptcha4Client.OnDialogShowListener dialogShowListener)

Parameter

Type

Description

dialogShowListener

AlicomCaptcha4Client.OnDialogShowListener

The listener object for when the verification window is displayed.

Configure extra parameters

public Builder setParams(Map<String, Object> params)

Parameter

Type

Description

params

JSONObject

The extra parameter settings. These parameters are passed to the JS.

The supported keys are as follows:

Value

Type

Description

displayMode

int

The theme style. Valid values:

  • 0 (default): Follows the system setting.

  • 1: Normal

  • 2: Dark

protocol

String

The protocol header. Supported protocols: HTTPS:// and HTTP://. Default value: HTTPS://.

bgColor

String

The background mask color for verification. This follows CSS rules. Note that the last two digits represent the opacity. Default value: #00000000.

loading

String

A custom loading animation. This is the relative path of the loading resource for the HTML file loaded by WebView. An empty string cancels the loading effect.

rem

float

Sets the overall scaling ratio of the Captcha.

hideBar

JSONArray

Hides the Close and Refresh buttons on the subsequent verification interface.

nextWidth

String

The width of the Captcha pop-up window. You must include the unit, such as "300px".

riskType

String

If risk control integration is configured on the server-side, this field can specify the verification type.

Important

This parameter does not take effect because the risk control integration mode is not yet available.

hideSuccess

boolean

Hides the success pop-up window for verification.

Code example

Map<String, Object> params = new HashMap<>();
params.put("displayMode", 0);
params.put("protocol", "https://");
params.put("bgColor", "#CC0000FF");
params.put("loading", "./loading_test.gif");
params.put("rem", 0.6);
JSONArray hideBar = new JSONArray()
         .put("close")
         .put("refresh");
params.put("hideBar", hideBar);
params.put("nextWidth", "300px");
params.put("hideSuccess", true);

AlicomCaptcha4Config.Builder builder = new AlicomCaptcha4Config.Builder()
        .setParams(params);

Construct the AlicomCaptcha4Config object

Constructs an AlicomCaptcha4Config object to pass to the init() method.

public AlicomCaptcha AlicomCaptcha4Config build()

Callback listener class APIs

Verification API callback

void onSuccess(Boolean status, String response)

Parameter

Type

Description

status

Boolean

The result of the user's verification operation.

  • true: Success.

  • false: Failed. If the verification fails, the Captcha style is refreshed.

response

String

If the user's verification operation is successful, you must obtain the response data and send it to the server-side for validation.

Successful verification example

{
  "lot_number":"b150e61198924eff9eaae121e9b67aa8",
  "pass_token":"6fcfba4c35cb57b873c99c5b3b02089c58a26244c18f7706aaa1e87aed3aa8f4",
  "gen_time":"1622020720",
  "captcha_output":"Tu6rW0dRNO0NLxMKftwdH00G5whF5HHddthoXSEA1G4="
}

Failed verification example

{
  "captchaId":"647f5ed2ed8acb4be36784e01556bb71",
  "captchaType":"slide",
  "challenge":"95ed53b4-95c6-4c6b-9aac-769b29ef74a0"
}

Loading failure callback

void onFailure(String error)

Parameter

Type

Description

error

String

A description of the verification loading failure, including cancellation by the user.

Loading failure example

{
  "code":"-14460",
  "msg":"Verification session cancelled",
  "desc":
       {"description":"User cancelled'Captcha'"}
}

Successful frontend loading

This callback is triggered after the verification frontend loads successfully. If preloading is complete, the callback is triggered immediately after you call verifyWithCaptcha.

void onWebViewShow()

Callback before the verification window is displayed

void actionBeforeDialogShow(Dialog dialog)

Parameter

Type

Description

dialog

Dialog

The verification window instance.

Callback after the verification window is displayed

You can change the system style in this callback, such as hiding the navigation bar or status bar.

void actionAfterDialogShow(Dialog dialog)

Parameter

Type

Description

dialog

Dialog

The verification window instance.

Callback when the verification window focus changes

void onDialogFocusChanged(Dialog dialog, boolean hasFocus)

Parameter

Type

Description

dialog

Dialog

The verification window instance.

hasFocus

boolean

Indicates whether the window has focus.