无法将其投放到Google reCAPTCHA中的Executor中

时间:2019-03-10 14:51:54

标签: java android

我在使用Google Recaptcha时遇到问题。 我正在使用https://developer.android.com/training/safetynet/recaptcha#java中的示例,但是尝试将this强制转换为Executor时出现错误。

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

我尝试加注Executor,但随后Android Studio强迫我包含execute(Runnable),并且验证码总是在此处结束,而不是在onSuccess()onFailure()中。

1 个答案:

答案 0 :(得分:0)

实际上,我不确定为什么在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());
                    }
                }
            });