Method returns null

Here is my layout code

static  String userLocation(String url) {

    RequestQueue queue = Volley.newRequestQueue(context);
    // String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";

     StringRequest dr = new StringRequest(Request.Method.GET, url, 
                new Response.Listener<String>() 
                {
                    @Override
                    public void onResponse(String response) {
                        // response
                        res=response;
                        //edit_location.setText(response);
                       // Toast.makeText(context, response, Toast.LENGTH_LONG).show();
                    }
                }, 
                new Response.ErrorListener() 
                {
                     @Override
                     public void onErrorResponse(VolleyError error) {
                         // error.
                         Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
                         Log.d("error", ""+error);
                   }
                }
            );
            queue.add(dr);
            return res;
 }

When I push the answer to the same method, then the answer prints fine and when I call the method and print the return statement, then res prints null.

Any help is appreciated. Thank you in advance.

Please suggest me how we can share answers in one or more actions.

And sorry for my bad english ..

+4
source share
2 answers

Try declaring res as a global variable. No return value. Because you did not know when the request would be completed. And use this global variable.

0
source

, onResponse()

+1

All Articles