在Android中解析Google Places JSON数据?[英] Parsing Google Places JSON data in Android?

本文是小编为大家收集整理的关于在Android中解析Google Places JSON数据?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试解析Google将JSON数据放在Android中.我知道我要通过工作的URL(但是我从以下代码中取出了钥匙).我可以将JSON数据显示在文本视图中,但是现在我想对其进行解析,并(此时)要显示的第一个项目的名称.我修改了一个解析Twitter提要的教程,并且该TwittWr教程正确显示,但是我的修改后的代码却没有显示.它不会崩溃,我没有任何错误,所以我不知道怎么了.所以这是我的代码:

    public class GetPort extends Activity {

TextView showJSdata;
HttpClient client;
JSONObject json;

final static String pURL = "https://maps.googleapis.com/maps/api/place/search/json? location=30.487263,-97.970799&radius=25000&types=airport&sensor=false&key=MY_KEY";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mn_test3);
    showJSdata = (TextView)findViewById(R.id.http_tv);
    client = new DefaultHttpClient();
    new ReadURL().execute("name");
}

public JSONObject showPorts() 
throws ClientProtocolException, IOException, JSONException{

    StringBuilder portURL = new StringBuilder(pURL);        
    HttpGet get = new HttpGet(portURL.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject lastport = timeline.getJSONObject(0);
        return lastport;
    }else{
        Toast.makeText(GetPort.this, "oops", Toast.LENGTH_SHORT);
        return null;
    }

}

public class ReadURL extends AsyncTask<String, Integer, String>{

    @Override
    protected void onPostExecute(String result) {
        showJSdata.setText(result);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            json = showPorts();
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return null;
    }

}

}

任何人都可以看到代码失败的地方或我出了什么问题吗?

推荐答案

好吧,我对您的代码进行了一些更改,并得到以下结果:

Cedar Park Regional Medical Center

如果这是应该给出的,则代码是:

if(status == 200){
    HttpEntity e = r.getEntity();
    String data = EntityUtils.toString(e);
    JSONObject jsonObject = new JSONObject(data);
    JSONArray timeline = jsonObject.getJSONArray("results");
    JSONObject lastport = timeline.getJSONObject(0);
    return lastport;
}

您可以通过将其粘贴到浏览器的地址线上来看到整个输出. 另请注意,在URL中的json?部分之后,您在那里有一个空间. 您首先需要从数据创建对象,然后对其进行解析.以results为单位的数组名称 然后在其中获取任何对象,然后取字字符串.

另外,当我运行您的代码(使用我的API键)时,我确实遇到了错误,不是致命的错误会崩溃您的应用程序,而是在解析JSON中的错误(在LogCat中出现在Orange中)

其他推荐答案

我很惊讶您没有遇到任何错误.

从这个文档,JSON响应应该是一个对象,而不是一个对象大批.您不能只采用解析Twitter响应的代码并将其应用于其他格式.

我没有手头的Google API键,您能提供您获得的数据的示例吗?如果与文档匹配,则需要这样的东西:

JSONObject responseOb = new JSONObject(data);
JSONArray results = responseOb.getJSONArray("results");
String firstAddress = results.getJSONObject(0).getString("formatted_address");

本文地址:https://www.itbaoku.cn/post/102625.html

问题描述

I'm trying to parse Google Places JSON data in Android. I know the URL I'm passing in works (but I took my key out of the below code). I can get the JSON data to display in a TextView, but now I want to parse it and get (at this point) the first item's name to display. I modified a tutorial that parsed a twitter feed, and that twittwr tutorial displayed correctly, but my modified code displays nothing. It doesn't crash, and I don't get any errors, so I don't know what's wrong. So here's my code:

    public class GetPort extends Activity {

TextView showJSdata;
HttpClient client;
JSONObject json;

final static String pURL = "https://maps.googleapis.com/maps/api/place/search/json? location=30.487263,-97.970799&radius=25000&types=airport&sensor=false&key=MY_KEY";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mn_test3);
    showJSdata = (TextView)findViewById(R.id.http_tv);
    client = new DefaultHttpClient();
    new ReadURL().execute("name");
}

public JSONObject showPorts() 
throws ClientProtocolException, IOException, JSONException{

    StringBuilder portURL = new StringBuilder(pURL);        
    HttpGet get = new HttpGet(portURL.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject lastport = timeline.getJSONObject(0);
        return lastport;
    }else{
        Toast.makeText(GetPort.this, "oops", Toast.LENGTH_SHORT);
        return null;
    }

}

public class ReadURL extends AsyncTask<String, Integer, String>{

    @Override
    protected void onPostExecute(String result) {
        showJSdata.setText(result);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            json = showPorts();
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return null;
    }

}

}

Can anyone see where the code fails, or where I went wrong?

推荐答案

Well, I changed your code a bit and got the following result:

Cedar Park Regional Medical Center

If that's what it's suppose to give, the code is this:

if(status == 200){
    HttpEntity e = r.getEntity();
    String data = EntityUtils.toString(e);
    JSONObject jsonObject = new JSONObject(data);
    JSONArray timeline = jsonObject.getJSONArray("results");
    JSONObject lastport = timeline.getJSONObject(0);
    return lastport;
}

You could see the entire output by pasting it to the address line of your browser. also note you have a space in there after the json? part in the url. You need first to create an object from the data, then parse it. Taking the array names as results then getting any objects in that, and taking its string.

Also, when I ran your code (with my API key) I did get errors, not fatal error which crashes your app, but error in parsing JSON (appeared in orange in the logcat)

其他推荐答案

I am surprised you are not getting any error.

Judging from this documentation, the JSON response should be an object, not an array. You cannot just take the code that parses twitter response and apply it to a different format.

I don't have a google api key at hand, could you provide an example of data you are getting? If it matches the documentation, then you need something like this:

JSONObject responseOb = new JSONObject(data);
JSONArray results = responseOb.getJSONArray("results");
String firstAddress = results.getJSONObject(0).getString("formatted_address");