2

I am using volley connection in local host and have a php file in the local host. Below is my class VolleyConnectionLogin and I have also a volley singleton class(which has no error)..

public class VolleyConnectionLogin {

    Context context;
    public VolleyConnectionLogin(Context context){
        this.context = context;
    }

    static Boolean check;
    public Boolean volleyConnection(final String username , final String password) {
        String tag_string_req = "string_req";
        String url = "http://192.168.10.8/login/forlogin.php";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, response.toString());
                        if(response.toString().equals("successful")){
                            check = true;
                        }else{
                            check = false;
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                check = false;
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("uname", username);
                params.put("pword", password);
                return params;
            }

        };
        VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
        return check;
    }
}
bansi
  • 55,591
  • 6
  • 41
  • 52
  • `check` variable is always false because you are setting the value of `check` variable in the `onResponse` method which is callback method. – ak sacha Feb 28 '17 at 06:19
  • intialize check with false like this `static Boolean check=false;` because default value of `Boolean` type is `null`. – Jinesh Francis Feb 28 '17 at 06:31

2 Answers2

1

check is null because the request is asynchronous, so when the following line is called

VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);

the program then proceeds to the next line which is

return check; // (which is still null) 

while another thread in the background started processing the request.

You could modify the request to be synchronous or modify your code so that whatever you have depending on the return of this method happens in the onResponse() block in the Response.Listener callback instead.

Community
  • 1
  • 1
Carl Poole
  • 1,970
  • 2
  • 23
  • 28
1

since you are returning boolean outside of your thread in which it receives response, this one

 Log.d(TAG, response.toString());
                        if(response.toString().equals("successful")){
                            check = true;
                        }else{
                            check = false;
                        }
                    }

control goes to return check; before the response return.

Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25