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?