0

I would like to check the user existence and return the result, however the onResponse() does not implement before returning the isUserExist, could anyone can advise how to fix? :)

// Check existence of email address
private boolean verifyEmail(String email) {
    isUserExist = false;

    loginURL = Uri.parse(loginURL).buildUpon()
            .appendQueryParameter("email",email)
            .build().toString();

    GsonRequest<StaffUser> gsonRequest = new GsonRequest<StaffUser>(loginURL, StaffUser.class, null,
            new Response.Listener<StaffUser>() {
                @Override
                public void onResponse(StaffUser response) {
                    if (response != null) {
                        Log.i(TAG, "staffUserId: " + response.getStaffUserId());
                        isUserExist = true;
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO: Handle error
            Log.e(TAG, "VolleyError: " + error);
        }
    }
    );

    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(gsonRequest);

    return isUserExist;
}
Stephen Chu
  • 15
  • 1
  • 7

2 Answers2

1

you have to create interface and pass listner to verifyEmail method that provide verify response in callback method.

Add interface for callback

public interface VerifyEmailListner {
    void onResponse(boolean success);
}

change your method like this,set void return type instead of boolean

private void verifyEmail(String email,final VerifyEmailListner listner) {
    isUserExist = false;

    loginURL = Uri.parse(loginURL).buildUpon()
            .appendQueryParameter("email",email)
            .build().toString();

    GsonRequest<StaffUser> gsonRequest = new GsonRequest<StaffUser>(loginURL, StaffUser.class, null,
            new Response.Listener<StaffUser>() {
                @Override
                public void onResponse(StaffUser response) {
                    if (response != null) {
                        Log.i(TAG, "staffUserId: " + response.getStaffUserId());
                        listner.onResponse(true);
                        isUserExist = true;
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO: Handle error
            Log.e(TAG, "VolleyError: " + error);
            listner.onResponse(false);
            isUserExist = false;
        }
    }
    );
    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(gsonRequest);
}

call method like this

  verifyEmail("abc@gmail.com",new VerifyEmailListner () {
        public void onResponse(boolean success){
            if(success){
                // verified
            } else{
              // not verified
            }
        }
    });
Ankit
  • 1,068
  • 6
  • 10
  • Hi Ankit, thanks a lot for your kind support! :) I just got the problem cannot return the result inside the callback function, could you advise? :) Many thanks!! `protected Boolean doInBackground(Void... params) { ... verifyEmail(mEmail, new VerifyEmailListner () { public boolean onResponse(boolean success){ if (success) { return true; } else{ return false; } } });` – Stephen Chu Sep 27 '19 at 05:42
  • dont request verifyEmail inside doInBackground – Ankit Sep 27 '19 at 05:58
  • I see, I use the retrofit to make the http direct call finally, many thanks for your answer!! :) – Stephen Chu Sep 27 '19 at 08:16
0

You have to use listener for Response detection Because Volly is Asyncronous

https://developer.android.com/training/volley

  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.

So you have to use listener handle responce see how to do it

  public class MyCustomListnerObject {
      // Step 1 - This interface defines the type of messages I want to communicate to my owner  
      public interface MyCustomObjectListener {
          // These methods are the different events and 
          // need to pass relevant arguments related to the event triggered
          public void onObjectReady(String title);
          // or when data has been loaded
          public void onDataLoaded(SomeData data);
      }

    private MyCustomObjectListener listener;

  // Constructor where listener events are ignored
    public MyCustomObject() {
        // set null or default listener or accept as argument to constructor
        this.listener = null; 
    }

    // Assign the listener implementing events interface that will receive the events
    public void setCustomObjectListener(MyCustomObjectListener listener) {
        this.listener = listener;
    }
    }

then your Volly

  // Check existence of email address
    private void verifyEmail(String email) {
        isUserExist = false;

        loginURL = Uri.parse(loginURL).buildUpon()
                .appendQueryParameter("email",email)
                .build().toString();

        GsonRequest<StaffUser> gsonRequest = new GsonRequest<StaffUser>(loginURL, StaffUser.class, null,
                new Response.Listener<StaffUser>() {
                    @Override
                    public void onResponse(StaffUser response) {
                        if (response != null) {
                            Log.i(TAG, "staffUserId: " + response.getStaffUserId());
                         if (listener != null)
                      listener.onDataLoaded(data); // <---- fire listener here
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO: Handle error
listener.onDataLoaded(data); // <---- fire listener here
                Log.e(TAG, "VolleyError: " + error);
            }
        }
        );

        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(gsonRequest);


    }

How to create a proper Volley Listener for cross class Volley method calling

Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
  • please consider posting some code as an example, even if it's just from the documentation. links don't stand the test of time (they can be moved, changed, content changed, etc) – a_local_nobody Sep 27 '19 at 05:26
  • I see, I use the retrofit to make the http direct call finally, many thanks for your answer!! :) – Stephen Chu Sep 27 '19 at 08:16