1

My question is about a strange behavious I notice both on my iPhone device and the codenameone simulator (NetBeans).

I invoke the following code below which calls a google web service to provide a list of food places around a GPS coordinate:

The web service that is called is as follows (KEY OBSCURED):

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXXXXXXXXXXXXXXXXX

Each result contains the next page token and thus, the second call (for the subsequent page) is as follows:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXXXXXXXXXXXXXXXXX&pagetoken=YYYYYYYYYYYYYYYYYY

public static byte[] getWSResponseData(String urlString, boolean usePost)
{
    ConnectionRequest r = new ConnectionRequest();

    r.setUrl(urlString);
    r.setPost(usePost);


    InfiniteProgress prog = new InfiniteProgress();
    Dialog dlg = prog.showInifiniteBlocking();
    r.setDisposeOnCompletion(dlg);
    NetworkManager.getInstance().addToQueueAndWait(r);

    try
    {
        Thread.sleep(2000);
    }
    catch (InterruptedException ex)
    {
    }

    byte[] responseData = r.getResponseData();


    return responseData;       
}



public static void  getLocationsList(double lat, double lng)
{
    boolean done = false;

    while (!done)
    {
        byte[] responseData = getWSResponseData(finalURL,false);

        result = Result.fromContent(parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(responseData))));
        String venueNames[] = result.getAsStringArray("/results/name");

        nextToken = result.getAsString("/next_page_token");

        if ( nextToken == null || nextToken.equals(""))
            done = true; 
        else
            finalURL = completeURL + "&pagetoken=" + nextToken;
    }
   .....
}

This code works fine with the sleep timer, but when I remove the Thread.sleep, only the first page gets called.

Any help would be appreciated.

Using the debugger does not help as this is a timing issue and the issue does not occur when using the debugger.

Also when I put some print statements into the code

while (!done)
{
   String nextToken = null;
   **System.out.println(finalURL);**
   ...
}
System.out.println("Total Number of entries returned: " + itemCount);

I get the following output:

First Run (WITHOUT SLEEP): https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXX

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXX&pagetoken=CqQCF... Total Number of entries returned: 20

Using the network monitor I see that the response to the second WS call returns:

{
  "html_attributions" : [],
  "results" : [],
  "status" : "INVALID_REQUEST" 
}

Which is strange as when I cut and paste the WS URL into my browser, it works fine...

Second Run (WITH SLEEP):

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXXX

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXXX&pagetoken=CqQCFQEAA...

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.714353,-74.00597299999998&radius=200&types=food&key=XXXXXXXXX&pagetoken=CsQDtQEAA... Total Number of entries returned: 60

Max R
  • 31
  • 3
  • Since this happens in the simulator what's the difference in the second round? What doesn't happen in the debugger? Do you see the second request going out in the network monitor tool? Where is it stuck? I would suggest avoiding `setDisposeOnCompletion` and instead invoking `dispose()` on the dialog after `addToQueueAndWait` finishes. – Shai Almog Nov 21 '17 at 06:48
  • Hi Shai, thank you for the quick response. Sorry I should have mentioned before, but the issue does not occur when I use the debugger (that is how I understood that it has something to do with timing). – Max R Nov 21 '17 at 16:39
  • I also removed the setDisposeOnCompletion as you suggested and added some print statements output to the code (I added the output to the overall question) – Max R Nov 21 '17 at 16:48

2 Answers2

1

Well it seems to be a google API issue as indicated here:

Paging on Google Places API returns status INVALID_REQUEST

I still could not get it to work by changing the WS URL with a random parameter as they suggested, but I will keep trying and post something here if I get it to work. For now I will just keep a 2 second delay between the calls which seems to work.

Max R
  • 31
  • 3
  • Notice that if the sleep is on the EDT this is a very bad thing that will cause a freeze in your UI. You would be able to workaround it by wrapping the sleep in `invokeAndBlock`. – Shai Almog Nov 22 '17 at 05:17
0

Well gave up on using the google WS for this and switched to Yelp, works very well:

https://api.yelp.com/v3/businesses/search?.....

Max R
  • 31
  • 3