4

Here's 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 toast the response on same method then response prints fine and when I call method and print return statement then res prints null.

Any help is appreciated. Thank you in advance.

Please suggest me how we can share response within a one or more activity

And sorry for my bad English..

Bishnu Kumar
  • 131
  • 1
  • 4
  • 17
  • I don't know your function `StringRequest`, but my guess is that it's an asynchronous function, so you're returning res before the value as been set. – Damien R. Nov 26 '13 at 08:25
  • This is a asynchrous task, please read more about that. You do not want to return a value from a interface, beacause it is fired asynchrous – A.S. Nov 26 '13 at 08:25
  • code is working fine but method not return value – Bishnu Kumar Nov 26 '13 at 09:57

2 Answers2

1

this is an asynchronous request, the volley processes the request in background thread and delivers the response in onResponse()

if you're looking for synchronous response try this

Community
  • 1
  • 1
Rajesh Batth
  • 1,672
  • 2
  • 19
  • 23
0

Try to declare res like global variable. Not return value. Because you didn't know, when request will finished. And use this global variable.

none
  • 1,699
  • 2
  • 13
  • 18
  • Of course it not return value, because when you return "res" you don't have "response" yet. Use global variable or something like BroadcastReceiver after recieve "response" to return value to App thread. – none Nov 26 '13 at 10:03
  • plz help me i am not able to share response within a activity – Bishnu Kumar Nov 27 '13 at 06:10