4

I have a problem with google recaptcha. I'm using theirs example from https://developer.android.com/training/safetynet/recaptcha#java but I get error when I try casting this into Executor.

Error which I get is: com.johny.Aktivity.Activity cannot be cast to java.util.concurrent.Executor.

I tried impementing Executor but then Android Studio forces me to include execute(Runnable) and recapcha always ends up there and not in the onSuccess() or onFailure().

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

9

Actually, I'm not sure that why the code shown at android developer site is not working, but you can try below code in which I have just used different methods for success and failure listeners.

 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());
                    }
                }
            });
Rikin Prajapati
  • 1,913
  • 2
  • 16
  • 23