Android SDK integration

SDK integration

Get the SDK

Download the aar package for verification code sdk from github Click to download

Manually import the SDK

Put the obtained aar file of sdk into the libs folder of the project, and then add the following code to the app's build.gradle file.

repositories {
    flatDir {
        dirs 'libs'
    }
}

Add a reference to the aar package in the dependencies dependency

compile(name:'captcha-release', ext: 'aar') 
//aar name and version number are subject to the latest version downloaded

SDK interface

Captcha property configuration class:CaptchaConfiguration

final CaptchaConfiguration configuration = new CaptchaConfiguration.Builder()
     .captchaId(noSenseCaptchaId)// captchaid
     .url(captchaUrl) // The accessor does not need to be set, the interface is the debug interface.
      // Captcha type, the default is the Normal Captcha. 
      // If you want to use Senseless captcha, please set the following types, otherwise please do not set
     .mode(CaptchaConfiguration.ModeType.MODE_INTELLIGENT_NO_SENSE)
     .listener(captchaListener) // captcha callback listener
     .timeout(1000 * 10) // Timeout, generally no need to set
     .languageType(langType) // captcha language type, default chinese, please set the value please refer to the following verification code language enumeration class introduction
     .debug(true) // Whether to enable debug mode, generally do not need to set
     // Set the position and width of the captcha frame
     .position(-1, -1, 0, 0)
     // Customize the different status pictures of the  slider
     .controlBarImageUrl(controlBarStartUrl, controlBarMovingUrl, controlBarErrorUrl)
     .backgroundDimAmount(dimAmount) // Verify the mask mask layer transparency, generally no need to set
     .touchOutsideDisappear(isTouchOutsideDisappear)  // Click the blank area captcha to disappear, the system default configuration is to disappear, set false does not disappear
     .useDefaultFallback(true) // Whether to use the default downtime scheme, it is enabled by default.
     .failedMaxRetryCount(failedMaxRetryCount) // The maximum number of attempts to load when a service is unavailable. If the number of times exceeds this number, the failure will trigger a downgrade. The default is 3 times.
     .build(context); // Context,please use the Context of the `Activity` instance

Captcha language enumeration class

In the above construction of the captcha attribute configuration class CaptchaConfiguration languageType attribute, its value is CaptchaConfiguration.LangType type, you can use the following values

 public static enum LangType {
        LANG_ZH_CN, // Chinese
        LANG_ZH_TW, // Traditional chinese
        LANG_EN,    // English
        LANG_JA,    // Japanese
        LANG_KO,    // Korean
        LANG_TH,    // Thai
        LANG_VI,    // Vietnamese
        LANG_FR,    // French
        LANG_AR,    // Arabic
        LANG_RU;    // Russian 
        LANG_DE,    // German
        LANG_IT,    // Italian
        LANG_HE,    // Hebrew
        LANG_HI,    // Hindi
        LANG_ID,    // Indonesian
        LANG_MY,    // Burmese
        LANG_LO,    // Laotian
        LANG_MS,    // Malay
        LANG_PL,    // Polish
        LANG_PT,    // Portuguese
        LANG_ES,    // Spanish
        LANG_TR,    // Turkish

        private LangType() {
        }
    }

Captcha feature provider class:Captcha

Captcha captcha = Captcha.getInstance().init(configuration);
captcha.validate();

Integration instructions

Initialization

 // Create a captcha callback listener
 captchaListener = new CaptchaListener() {
            @Override
            public void onReady() {

            }

            @Override
            public void onValidate(String result, String validate, String msg) {
                if (!TextUtils.isEmpty(validate)) {
                    Toast.makeText(getApplicationContext(), "success:" + validate, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "failure", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onError(int code, String msg) {
                Log.e("Captcha", "error,code:" + code + " message:" + msg);
                Toast.makeText(getApplicationContext(), "error,code:" + code + " message:" + msg, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onClose() {
                Toast.makeText(getApplicationContext(), "User closes captcha", Toast.LENGTH_LONG).show();
            }
        };
	
// Create a configuration class that builds the captcha. For detailed options, see the SDK interface above. configuration class:CaptchaConfiguration
final CaptchaConfiguration configuration = new CaptchaConfiguration.Builder()
                        .captchaId(noSenseCaptchaId)// captchaID
                        .mode(CaptchaConfiguration.ModeType.MODE_INTELLIGENT_NO_SENSE)  // captcha type,default normal captcha,if you want use smart capcha,please set this type. 
                        .listener(captchaListener) //Set the captcha callback listener
                        .build(context); // Context,please use the Context of the `Activity` instance
// Initialization captcha
final Captcha captcha = Captcha.getInstance().init(configuration);

NOTE:If the business id you applied for on the official website of YiDun is smart capcha type, please be sure to set the mode parameter, otherwise please do not set

captcha.validate();

Confused configuration

proguard confused configuration file increase:

-keepattributes *Annotation*
-keep public class com.netease.nis.captcha.**{*;}

-keep public class android.webkit.**

-keepattributes SetJavaScriptEnabled
-keepattributes JavascriptInterface

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

Common problem

js error not found onValidate

"Uncaught TypeError: JSInterface.onValidate is not a function", source:xxxx

Reason: JSInterface is confused, please refer to the [confused configuration] keep captcha related class.

Captcha falied to load

Reason: If the generated url address is accessible and displayed on the computer, it cannot be accessed on the mobile browser, and the browser displays "Your clock is slow", indicating that the phone system time does not match the certificate time. The main problem is that the phone that was taken during the test may have problems.

Captcha failed to load,log print:"Uncaught TypeError: Object [object Object] has no method

"Uncaught TypeError: Object [object Object] has no method 'onReady'", source:xxxx

Reason: The annotations @JavascriptInterface must be added when using the JS interface after Android 4.2, but sometimes it will be confused, and the obfuscated configuration is added:

-keepattributes *Annotation*

Please refer to the [Confused Configuration] section.

Online Chat Tel:95163223