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

查看:94
本文介绍了解析谷歌地方JSON数据在Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析谷歌地方JSON数据在Android中。我知道我传递的作品网址(但我把我的钥匙出了低于code)。我可以得到JSON数据到一个TextView显示,但现在我要分析它,并得到(此时)的第一个项目的名称显示。我修改了一个教程,解析的Twitter的饲料,并显示twittwr教程正确的,但我修改code无显示。它不会崩溃,我没有得到任何错误,所以我不知道什么是错。因此,这里是我的code:

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;
    }

}

}

任何人都可以看到的code失败,或者我哪里错了?

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

推荐答案

嗯,我改变了你的codeA位,得到以下结果:

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

Cedar Park Regional Medical Center

如果这就是它想给的code是这样的:

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;
}

您可以看到整个输出其粘贴到浏览器的地址栏中。 还要注意你的 JSON?部分,在URL后有一个空格在里面。 您需要首先创建从数据的对象,然后解析它。以数组名称作为结果 然后凑的任何对象,并采取它的字符串。

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.

此外,当我跑你code(与我的API密钥),我没有得到的解析JSON(出现在橘子中的logcat中)错误,而不是致命错误而崩溃的应用程序,但误差

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)

这篇关于解析谷歌地方JSON数据在Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆