Each GooglePlaces Instance Can Only Perform 2 getNearbyLocations Calls
While experimenting with the library, I found that on the third getNearbyLocations call the GooglePlaces object hangs the thread. However, if I instead initiate a new GooglePlaces object every 2 calls, the objects work fine.
Here is the code I used to test this. The coordinates were chosen due to the large amount of nearby restaurants. I have found that sameInstanceTest() hangs after 2 print statements, while multipleInstanceTest() outputs 5 print statements and exits.
public void multipleInstanceTest(){
GooglePlaces gpl = new GooglePlaces(API_KEYS[0]);
int usages = 0;
double[][] testDoubles = new double[][]{
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695}
};
double range = 6000;
for (double[] coords : testDoubles){
List<Place> places = new ArrayList<>();
try {
places = gpl.getNearbyPlaces(coords[0], coords[1],range, 20, Param.name("type").value("food"));
} catch (Exception e){
//Default to 0
}
usages ++;
System.out.printf("Got %d locations at coords %f,%f\n",places.size(),coords[0],coords[1]);
if (usages == 2){
gpl = new GooglePlaces(API_KEYS[0]);
usages = 0;
}
}
}
public void sameIstanceTest(){
GooglePlaces gpl = new GooglePlaces(API_KEYS[0]);
double[][] testDoubles = new double[][]{
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695},
new double[]{40.7607077,-73.980695}
};
double range = 6000;
for (double[] coords : testDoubles){
List<Place> places = new ArrayList<>();
try {
places = gpl.getNearbyPlaces(coords[0], coords[1],range, 20, Param.name("type").value("food"));
} catch (Exception e){
}
System.out.printf("Got %d locations at coords %f,%f\n",places.size(),coords[0],coords[1]);
}
}
Same thing is happening to me. For ZERO_RESULTS only. I noticed that in this case an exception is thrown (NoResultsFoundException). I'm guessing the connection is not closed in this case and the apache http connection pool is depleted after two requests.
Sorry, I was using 2.1.3. The problem no longer exists in 2.1.7