8

I am using Android Studio 3

I am following this article to learn how to use Google Recaptcha in Android Studio.

Installed the package using this: implementation 'com.google.android.gms:play-services-safetynet:12.0.1'

API keys are also registered.

I saw there is onClick event handler but where is it mentioned about rendering the recaptcha?

Update 1

When I wrote the button click code as mentioned in the link...I got a complication error: inconvertible types cannot cast anonymous android.view.view.onclicklistener to java.util.concurrent.executor

Code as asked in comment

btn_Login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        SafetyNet.getClient(this).verifyWithRecaptcha("")
            .addOnSuccessListener((Executor) 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((Executor) 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 {
                    }
                }
            });
    }
});
v.karbovnichy
  • 3,183
  • 2
  • 36
  • 47
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Can you paste your code here? – v.karbovnichy Apr 18 '18 at 21:49
  • This is same code as mentioned in the link. I followed everything as mentioned in your answer but facing above compilation error. **inconvertible types cannot cast anonymous android.view.view.onclicklistener to java.util.concurrent.executor** – Pankaj Apr 20 '18 at 22:57

2 Answers2

1

I used below code and everything is work fine now.

Make sure to implement Executor in the activity

btn_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 {
                    }
                }
            });
    }
});
Pankaj
  • 9,749
  • 32
  • 139
  • 283
-1

According to the article, in your button click handler you must call the method SafetyNet.getClient(this).verifyWithRecaptcha(...) to show reCAPTCHA and handle success or error. Passing this, you give the SDK handle to your current view which should be shown after solving reCAPTCHA. Most probably the rendering will be done by the SDK itself given that it’s a part of the OS. And most probably it will be full-screen in a separate top-level view blocking access to your app before solving the riddle.

You should try to implement it in your app as described in the article and see how it goes. Then you can ask a more specific question.

EDIT: You combined 2 techniques in your code: copy-pasting the code from Google and implementing anonymous class from it. So the problem you asked in the comment is that using (Executor) this in line 5 refers now not to your View (as it was there in the original tutorial) but to the instance of the anonymous interface implementation new View.OnClickListener() that you created. Ypu can refer to this answer to see how it can be implemented not interfering with already complex reCAPTCHA code.

v.karbovnichy
  • 3,183
  • 2
  • 36
  • 47