How to wait androidquery ajax finish

407 views Asked by At

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;    
        }    
2

There are 2 answers

4
Innovation On

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.

1
KumarA On

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.