* @param query Must be the category to search on a desired city
* @param nextPageToken initially must be ""
* @return the nextPageToken
*/
public String placesSearchBy(String category, String andCity, String nextPageToken, List<Place> listOfPlaces) {
Place p = new Place();
JsonFactory factory = new JsonFactory();
String type = category;
String url = "https://maps.googleapis.com/maps/api/place/textsearch/";
url += "json?query=" + category + "+in+" + andCity + "&sensor=false";
url += "&key=" + _key;
if (!nextPageToken.isEmpty()) {
url += "&pagetoken=" + nextPageToken;
nextPageToken = "";
}
try {
JsonParser parser = factory.createJsonParser(new URL(url));
// loop until token equal to "}"
while (parser.nextToken() != JsonToken.END_OBJECT) {
String fieldname = parser.getCurrentName();
if ("html_attributions".equals(fieldname)) {
parser.nextToken();
while (parser.nextToken() != JsonToken.END_ARRAY) {
parser.getText();
}
} else if ("next_page_token".equals(fieldname)) {
nextPageToken = parser.getText();
} else if ("results".equals(fieldname)) {
JsonToken token = parser.nextToken();
while ((token = parser.nextToken()) != JsonToken.END_ARRAY) {
String resultToken = parser.getText();
if (resultToken.equals("formatted_address")) {
token = parser.nextToken();
p.setAddr(parser.getText());
} else if (resultToken.equals("lat")) {
token = parser.nextToken();
p.setLat(Double.parseDouble(parser.getText()));
} else if (resultToken.equals("lng")) {
token = parser.nextToken();
p.setLng(Double.parseDouble(parser.getText()));
token = parser.nextToken();
token = parser.nextToken();
resultToken = parser.getText();
} else if (resultToken.equals("icon")) {
token = parser.nextToken();
String icon = parser.getText();
p.setIcon(icon);
type = icon.substring(icon.lastIndexOf("/")+1,icon.length()-7);
} else if (resultToken.equals("name")) {
token = parser.nextToken();
p.setName(parser.getText());
} else if (resultToken.equals("types")) {
while((token = parser.nextToken()) != JsonToken.END_ARRAY) {
if (token != JsonToken.START_ARRAY && token != JsonToken.END_ARRAY)
p.addTypes(parser.getText());
}
} else if (resultToken.equals("photos")) {
while((token = parser.nextToken()) != JsonToken.END_ARRAY){
resultToken = parser.getText();
if ("html_attributions".equals(resultToken)) {
parser.nextToken();
while (parser.nextToken() != JsonToken.END_ARRAY) {
parser.getText();
}
}
}
}
if (p.getTypes() != null && token == JsonToken.END_OBJECT) {
PlacesDao d = new PlacesDao(_url);
p.setCategory(type);
d.completeRecord(p);
if(p.getId() == null)
d.insertPlaces(p);
Place b = new Place();
b.setAddr(p.getAddr());
b.setCategory(p.getCategory());
b.setId(p.getId());
b.setIcon(p.getIcon());
b.setLat(p.getLat());
b.setLng(p.getLng());
b.setName(p.getName());
b.setTypes(p.getTypes());
listOfPlaces.add(b);
p = new Place();
d.close();
}
}
}
if ("status".equals(fieldname)) {