I recently implemented this code in an Android application to get information about nearby places. The Url implemented is taken directly covered by Google's documentation, more precisely from the Google Play Services API. The code works and the place below:
public class TabFragmentOne extends Fragment implements LocationListener {
...
@Override
public void onLocationChanged(Location location) {
String stringUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
+ location.getLatitude()+","+ location.getLongitude() +
"&radius=10" +
"&sensor=false" +
"&types=airport|hospital" +
"&key=ApiKey";
ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new DownloadWebpageTask().execute(stringUrl);
} else {
//textView.setText("No network connection available.");
}
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
if(result != null) {
AudioManager audiomanage = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
TextView t = (TextView) rootView.findViewById(R.id.place);
t.setText(result);
}
}
}
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
...
}
As I said the code works and the array of return is this:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 39.94120700000001,
"lng" : 18.350088
},
"viewport" : {
"northeast" : {
"lat" : 39.94122815,
"lng" : 18.35013080000001
},
"southwest" : {
"lat" : 39.94115994999999,
"lng" : 18.34999280000001
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/doctor-71.png",
"id" : "eca87bf283bf49e3092b9e69572a3ec1724e45e2",
"name" : "I.P.A.S.S. Servizi srl",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},
"photos" : [
{
"height" : 672,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/110822823161060200191/photos\"\u003eI.P.A.S.S. Servizi srl\u003c/a\u003e"
],
"photo_reference" : "CoQBcwAAAM5AfQw4U2o1kQil-iH-tR44IbQq96zOLSoGYUl5VzpLv_FMUNVio9f-7WU9GlHh2XsqzTcvtTrFoaBfi0a6FTtYaKJS0zfxMgUe9Mv-HX9DkoNFknKMlpdsJI4fBMKFxLEIRcpJv6LxV7Z5NUWyEgZG2gZagAxG_YsUUrWe8QpmEhA0NC7LJyoaGCIRWj4RSFGfGhTKQmurhcR3oxdoAvrBBT7GeM-D_w",
"width" : 1120
}
],
"place_id" : "ChIJf0jXZcENRBMRzuC0rCGQZE8",
"reference" : "CnRpAAAAc9SaUNq08kwWCqAjn8QhoLZYooy8G1LXc80VUPoK9oBg4bgdblyU7e8xVwLngrCMRvJzzfNOJ4ZCiCJ4qDpEdA5NnY1Nnirh7ImJUGRHMzP6HkE2OhX_2jlKZe6duwG2sDV5o2jpUD1dgim0eKCyExIQ5Q5H6kXYkQqvrh-YXrgufBoU05MYYPtsYLHlJkrc4HnA6niqrB0",
"scope" : "GOOGLE",
"types" : [ "hospital", "health", "point_of_interest", "establishment" ],
"vicinity" : "Via Luigi Galvani, Tricase"
}
],
"status" : "OK"
}
My question is this: How can I do to pick up every single element of return? For example to print the information contained in "icon".
I hope I was quite accurate as possible and thank you for help.