windy1 / google-places-api-java

Comprehensive and FULL Java client for the Google Places API
MIT License
171 stars 89 forks source link

Problem running getNearbyPlacesRankedByDistance multiple times #58

Open alxsfrm opened 7 years ago

alxsfrm commented 7 years ago

Hi, I'm trying to use your library in my project and encounters in following issue: i can't run the function getNearbyPlacesRankedByDistance more then two times. Here is my function:

public void SearchPlaces(Context ctx)
{
    new Thread(new Runnable() {
        public void run() {
            List<Place> places=null;
            try {
                Log.i("MyApp", "search started");
                places = client.getNearbyPlacesRankedByDistance(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), 20, Param.name("keyword").value(task_category.getSearchKey()));
                Log.i("MyApp", "places received");
            } catch (Exception e) {
                Log.i("MyApp", "exception"); 
            }
            Log.i("MyApp", "thread exit");
        }
    }).start();
}

On the third run i'm running my function, the function getNearbyPlacesRankedByDistance does not returns and seems to be blocked. After the function execution i don't get anything neither in the log, nor in debug. What am i doing wrong ?

Regards, Alex.

romualdr commented 7 years ago

I also have this behaviour with getPlacesByQuery.

Funny is that just like you your code, I create a singleton instance of Google Places.

When I create a Google Places instance with each call, it works well even after three call. I guess something is not well closed (or closed somehow after the second call ?)

romualdr commented 7 years ago

Found it !

In the file DefaultRequestHandler.java:58, the connection is not closed.

We could have used response.getEntity().getContent() which automatically closes the connection, but its deprecated, though we can use EntityUtils.consume(response.entity) after the line to close it.

I created my own version of RequestHandler with this line, gave it to new GooglePlaces(API_KEY, MyRequestHandler()) and everything works as expected.

Unfortunately, I don't have time to create a PR right now, but here's the solution !