【问题标题】:How to get json object of google maps places api in android?如何在android中获取google maps places api的json对象?
【发布时间】:2015-08-25 07:44:22
【问题描述】:

有什么方法可以从 google maps places api 在 android 中获取 json 数据。我正在使用以下链接检索数据。

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

它以 json 格式返回数据。请帮助我如何在android中获取上述数据。

【问题讨论】:

  • 添加返回数据的示例,以便您得到帮助

标签: android json google-maps


【解决方案1】:

我知道这是一个老问题。但我希望它将来对其他人有用。

首先,您必须将其添加到您的模块级别build.gradle

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

下一步是创建一个 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;
    }
}

然后从任何你想要的地方执行 AsyncTask。 (可能来自 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);

您可以在从 Google 地方信息获取地点时添加更多选项,例如 关键字语言 等。阅读更多关于这些选项以及如何使用它们的信息here

【讨论】:

  • 非常感谢。这对其他人会有所帮助:)
  • 不错。它不相关,但你说“也许来自 MainActivity”,究竟该怎么做?我试过.excute(),但是不知道如何获取onPostExecute()的返回值。
【解决方案2】:

也许GSON 可以帮助你。

【讨论】:

    【解决方案3】:

    查看 JSONObject JSON Parser Tutorial 以获取 JSON 数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2014-03-28
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多