1

My problem is, this method is returning the list before the ajax fill that.
How can I wait for the ajax response, before return the list?

GEOCODER:

public List<Address> getAddresses(String address) {     
                List<Address> addresses = new ArrayList<Address>();

                String url = "http://maps.googleapis...";

                AQuery aq = new AQuery(context);   

                aq.ajax(...); //Here I fill the addresses list

                return addresses;    
        }    
Bruno Pinto
  • 2,013
  • 3
  • 23
  • 33

2 Answers2

0

Put aq.ajax() in different method/function and when ajax call get successfully completed in that method/function,then return the results to this method/function and then return those results from this method/function.

Innovation
  • 1,514
  • 17
  • 32
  • I don't understand. What is the diference? to put that on ohter mehtod? I tried here and donsn't work. How can i know the ajax has done? – Bruno Pinto Nov 29 '13 at 11:17
  • I have tried this put the callback to ajax in another method, but do not work. – Bruno Pinto Nov 29 '13 at 17:12
  • It is different because.If ajax call remain in same method the function will work in such a way that it run ajax call and move to other line and would not wait for ajax call to complete.While if we put it in different method then this function would wait for that function to return something and then when control get back to this function then we can move to other statment execution. – Innovation Dec 02 '13 at 06:21
  • So current problem is code make ajax call and then it is executing the next statement and it is not waiting for ajax call completetion. – Innovation Dec 02 '13 at 06:22
0

Make a synchronous call, by default AJAX does a asynchronous call (asynchronous should be false).

In Async call, before receiving data from your requested url, it returns addresses.

Edit:

you can return addresses once received a response, else you can call sleep function and you can do async: false, but last one is not a recommended way.

KumarA
  • 1,368
  • 3
  • 18
  • 41