public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Read().execute("masjids");
}
public JSONObject retrieveInfo(String radius) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(
"http://iqamah.org/api/index.php?lat=43&long=-79&radius=");
url.append(radius);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(url.toString());
HttpResponse r = httpclient.execute(get);
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject timeline = new JSONObject(data);
return timeline.getJSONObject("1");
}
private class Read extends AsyncTask<String, Integer, String> {
ProgressDialog pd = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Loading...");
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
JSONObject json = retrieveInfo("200");
return json.getString(arg0[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;
}
@Override
protected void onPostExecute(String status) {
super.onPostExecute(status);
pd.dismiss();
TextView day = (TextView) findViewById(R.id.displayDay);
day.setText("Masjid: " + status);
}
}
}
的JSON:
{
"status": "ok",
"masjids": [
{
"id": "44|Madina Masjid|1200 Danforth Ave, Toronto|49.99"
},
{
"id": "39|Darul Taqwa|1 Thorncliffe Park Drive, Toronto|51.59"
},
{
"id": "43|Darul Khair|25 St Dennis, Toronto|52.12"
}
]
}
我試圖訪問masjids->id[0]
陣列,我怎麼能做到這一點與上面的代碼?
嗯..閱讀的JSONObject文檔以及如何使用它在陣列的情況下。 – 2013-03-01 04:48:48
檢查一下,我幾天前回答了一個類似的問題,你可以根據你的回答調整答案。 http://stackoverflow.com/questions/13576676/how-to-parse-complex-json-file-in-android/13576784#13576784希望它有幫助。 – Atrix1987 2013-03-01 05:48:34