Parsing Google Places JSON data in Android?

2019-04-02 13:14发布

问题:

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?

回答1:

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)



回答2:

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");