Exception using google-maps-services-java on await

I just try to make a nearby search request, filter the results and finally make a details search request for every place I got.

I do something like this:

    GeoApiContext geoApiContext = new GeoApiContext().setApiKey("my_key");
    NearbySearchRequest nearbySearchRequest = new NearbySearchRequest(geoApiContext);
    PlaceDetailsRequest placeDetailsRequest = new PlaceDetailsRequest(geoApiContext);

    try {
        nearbySearchRequest.location(new LatLng(latitude, longitude)).radius(1000);
        nearbySearchRequest.type(placeType);
        PlacesSearchResponse placesSearchResponse = nearbySearchRequest.await();
        Collections.addAll(places, placesSearchResponse.results);
    } catch (Exception e) {
        e.printStackTrace();
    }
    List<PlacesSearchResult> sortedPoints = filterPlaces(places);
    LOGGER.info(sortedPoints.size());
    try {
        for (PlacesSearchResult result : sortedPoints) {
            placeDetailsRequest.placeId(result.placeId);
            PlaceDetails placeDetails = placeDetailsRequest.await();
            objectOfInterests.add(convert(placeDetails));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

and here is the stack trace that prints the unit test:

abr 21, 2016 10:10:39 AM com.google.maps.GeoApiContext getWithPath
INFORMACIÓN: Request: https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIza...&location=-0.206238%2C-78.492243&radius=1000&type=lodging
abr 21, 2016 10:10:41 AM com.google.maps.GeoApiContext getWithPath
INFORMACIÓN: Request: https://maps.googleapis.com/maps/api/place/details/json?key=AIza...&placeid=ChIJd6N_BGma1ZERqZqNFkQ7hGU
10:10:41,416 INFO ObjectOfInterestService:77 - 3
java.lang.IllegalStateException: 'await', 'awaitIgnoreError' or 'setCallback' was already called.
    at com.google.maps.PendingResultBase.makeRequest(PendingResultBase.java:74)
    at com.google.maps.PendingResultBase.await(PendingResultBase.java:55)
    at com.guide.services.ObjectOfInterestService.queryGoogleByInterestType(ObjectOfInterestService.java:81)
    at com.guide.services.ObjectOfInterestServiceTest.testQueryGoogleByInterestType(ObjectOfInterestServiceTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The for loop only executes one time but the list has 3 elements. The exception stop the execution of the for loop.

How can I solve this?

Upvotes: 0

Views: 919

Answers (1)

Brett
Brett

Reputation: 2399

Try moving the PlaceDetailsRequest inside the loop. It is not re-usable.

try {
    for (PlacesSearchResult result : sortedPoints) {
        PlaceDetailsRequest placeDetailsRequest = 
            new PlaceDetailsRequest(geoApiContext);
        placeDetailsRequest.placeId(result.placeId);
        PlaceDetails placeDetails = placeDetailsRequest.await();
        objectOfInterests.add(convert(placeDetails));
    }
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 2

Related Questions