I\'m using Google places API and it is returning all the places in my location. However I only want it to return a type for \"car-repair\" I think I nearly have it but I\'m
I've prepared this for you , hope it helps
// A class to store your results
public class Place {
private String icon;
private Double latitude;
public void setIcon(String icon) {
this.icon=icon;
}
public void setLatitude(Double latitude) {
this.latitude=latitude;
}
// .....
}
// Utility class to keep things simple
public class Functions {
public static JSONObject convertInputStreamToJSONObject(InputStream inputStream)
throws JSONException, IOException
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return new JSONObject(result);
}
public static ArrayList parsePlacesFromJson(JSONObject datos) throws JSONException {
List placesList = new ArrayList<>();
// in your case this must be "results" I think
final int LIST_LENGTH = datos.getJSONArray("results").length();
//final int LIST_LENGTH = datos.getJSONArray("nameOfTheJsonMainArray").length();
Log.d("Length", String.valueOf(LIST_LENGTH));
// For every element in the list
for(int i = 0; i < LIST_LENGTH; i++) {
// Instance of a new Place
Place place = new Place();
// Get data as needed, this represents one place
JSONObject obj = datos.getJSONArray("results").getJSONObject(i);
Double latitude = obj.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
String icon = obj.getString("icon");
place.setLatitude(latitude);
place.setIcon(icon);
//....
placesList.add(place)
}
return placesList;
}
}
// The google places reader class
public class ApiReader {
private static final String TAG = "API_READER";
Context context;
View v;
public ApiReader(Context context, View v) {
this.context = context;
this.v = v;
}
private static final String APIKEY = "yourkeyhere";
private static String ENDPOINT =
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.27,-8.47&radius=1000&types=car_repair&key=%s";
// Method to be called
public void getCarRepairs() throws IOException {
URL url = new URL(String.format(ENDPOINT, APIKEY));
new GetPlaces().execute(url);
}
// Inner asyctask class
private class GetPlaces extends AsyncTask> {
List lista;
@Override
protected List doInBackground(URL... params) {
JSONObject datos;
URL url = params[0];
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
datos = Functions.convertInputStreamToJSONObject(in);
Log.i(TAG, datos.toString());
// De los datos obtenemos un objeto Place
lista = Functions.parsePlacesFromJson(datos);
Log.i(TAG, "" + lista.toString());
Log.i(TAG, "Went into try was OK.");
} catch (Exception e) {
Log.e(TAG, e.toString());
Log.i(TAG, "Went into catch");
} finally {
urlConnection.disconnect();
Log.i(TAG, "Went into finally, urlConnection disconnected.");
}
return lista;
}
// This method it gets what the "doInBackGround" returns
protected void onPostExecute(List placesList) {
if(placesList != null) {
// Do whatever with the list of places
//Functions.updateView(v, placesList);
}
}
}
}
// And to make things work in yout activity just do like this
ApiReader api = new ApiReader(this, findViewById(R.id.mainLayout));
try {
api.getCarRepairs();
} catch (IOException e) {
e.printStackTrace();
}
Enjoy !!