简体   繁体   中英

How to get json object of google maps places api in android?

Is there any way to get the json data from google maps places api in android. I am using the following link to retrieve data.

https://maps.googleapis.com/maps/api/place/textsearch/json?query=Store+in+Lahore&type=clothing_store&key=----

it returns data in json format. Please help me how can I get the data form the above in android.

I know it's an old question. But I hope it will be useful for others in future.

First, you have to add this to your module level build.gradle ,

android {
    useLibrary 'org.apache.http.legacy'
}

Next step is to create an AsyncTask,

public class MyAsyncTask extends AsyncTask<String, Void, Boolean> {
    private JSONObject jsonObject;

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        System.out.println(jsonObject); //use jsonObject here
    }

    protected Boolean doInBackground(final String... args) {
        try {
            Looper.prepare();
            String latitude = args[0];
            String longitude = args[1];
            String radius = args[2];
            String name = args[3];
            String key = "YOUR_API_KEY_FOR_BROWSER";

            String uri = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" 
                    + "location=" + latitude + "," + longitude 
                    + "&radius=" + radius 
                    + "&name=" + name
                    + "&key="+ key; // you can add more options here

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new UrlEncodedFormEntity(new ArrayList<NameValuePair>()));
            HttpEntity httpEntity = httpClient.execute(httpPost).getEntity();

            InputStream stream = httpEntity.getContent();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(stream, "utf-8"), 8);
            StringBuilder sBuilder = new StringBuilder();

            String line = null;
            while ((line = bReader.readLine()) != null) {
                sBuilder.append(line + "\n");
            }

            stream.close();
            jsonObject = new JSONObject(sBuilder.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

And then execute the AsyncTask from wherever you want. (Maybe from MainActivity),

String latitude = String.valueOf(latLng.latitude);
String longitude = String.valueOf(latLng.longitude);
String radius = "2000"; // 2 Kilometer
String name = "hospital";

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(latitude, longitude, radius, name);

You can add more option while fetching places from Google Places, for example keyword , language etc. Read more about those options and how to use them here .

也许GSON可以帮助您。

查看JSONObjectJSON Parser教程以获取JSON数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM