我的代码位于以下文章之后,在Android Studio 3中实现Recaptcha:https://developer.android.com/training/safetynet/recaptcha.html
btn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(this).verifyWithRecaptcha("api key")
.addOnSuccessListener((Executor) this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
}
}
})
.addOnFailureListener((Executor) this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
int statusCode = apiException.getStatusCode();
} else {
}
}
});
}
});
我面临下面的编译错误。
in-convertible类型:无法将匿名android.view.view.onclicklistener强制转换为java.util.concurrent.executor
我错过了什么吗?
答案 0 :(得分:1)
问题是this
。在这种情况下,this
是类View.OnClickListener
,它不是Activity
。
您应该像SafetyNet.getClient(YourActivity.this)
和(Executor) YourActivity.this
答案 1 :(得分:1)
我使用下面的代码,现在一切正常。
确保在活动
中实施Executorbtn_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
.addOnSuccessListener((Activity) MyActivity.this,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
// Indicates communication with reCAPTCHA service was
// successful.
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
})
.addOnFailureListener((Activity) MyActivity.this, 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();
} else {
}
}
});
}
});