我正在尝试在 Android 中解析 Google Places JSON 数据。我知道我传递的 URL 有效(但我从下面的代码中取出了我的密钥)。我可以让 JSON 数据显示在 TextView 中,但现在我想解析它并获取(此时)要显示的第一个项目的名称。我修改了一个解析 twitter 提要的教程,该 tittwr 教程正确显示,但我修改后的代码什么也没显示。它不会崩溃,我也没有收到任何错误,所以我不知道出了什么问题。所以这是我的代码:
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;
}
}
}
谁能看到代码失败的地方,或者我哪里出错了?