java - 无法在 google reCAPTCHA 中将其转换为 Executor
问题描述
我对谷歌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)
并且 recapcha 总是在那里结束,而不是在onSuccess()
oronFailure()
中。
解决方案
实际上,我不确定为什么 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());
}
}
});
推荐阅读
- node.js - 使用谷歌日历 API 并通过 Axios 获取 401 错误消息
- python - 更改 ONNX 模型的输入大小
- c# - 想用所有不同的长部分从最后一个到第一个分割字符串
- reactjs - 在redux连接组件中多次调用componentDidMount
- c++ - 如何使浮点数显示 -0 也显示为 0?
- python - 为什么这个字符串不附加一个 for 循环工作?
- javascript - 将 SVG 文件插入 HTML
- shopify - Shopify 产品列表中的变体
- terraform - 用于 100 个子网的 Terraform cidrsubnets 循环
- angular - 如何避免 Angular 11 中硬编码的 @HostListener 事件?