首页 > 解决方案 > 无法在 google reCAPTCHA 中将其转换为 Executor

问题描述

我对谷歌recaptcha 有疑问。我正在使用他们来自https://developer.android.com/training/safetynet/recaptcha#java的示例,但是当我尝试转换thisExecutor.

我得到的错误是:com.johny.Aktivity.Activity cannot be cast to java.util.concurrent.Executor

我尝试过执行Executor,但后来 Android Studio 强迫我包含execute(Runnable)并且 recapcha 总是在那里结束,而不是在onSuccess()oronFailure()中。

标签: javaandroid

解决方案


实际上,我不确定为什么 android 开发者网站上显示的代码不起作用,但您可以尝试下面的代码,其中我刚刚对成功和失败的侦听器使用了不同的方法。

 SafetyNet.getClient(this).verifyWithRecaptcha("YOUR_API_SITE_KEY")
            .addOnSuccessListener(new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                @Override
                public void onSuccess(SafetyNetApi.RecaptchaTokenResponse recaptchaTokenResponse) {
                    // Indicates communication with reCAPTCHA service was
                    // successful.
                    String userResponseToken = recaptchaTokenResponse.getTokenResult();
                    if (!userResponseToken.isEmpty()) {
                        // Validate the user response token using the
                        // reCAPTCHA siteverify API.
                        Log.e(TAG, "VALIDATION STEP NEEDED");
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        // An error occurred when communicating with the
                        // reCAPTCHA service. Refer to the status code to
                        // handle the error appropriately.
                        ApiException apiException = (ApiException) e;
                        int statusCode = apiException.getStatusCode();
                        Log.e(TAG, "Error: " + CommonStatusCodes
                                .getStatusCodeString(statusCode));
                    } else {
                        // A different, unknown type of error occurred.
                        Log.e(TAG, "Error: " + e.getMessage());
                    }
                }
            });

推荐阅读