JSON Parser没有检索对象

时间:2014-04-26 20:50:46

标签: java android json parsing

我有一个活动应该从这个页面获得纬度。 http://zen.coderdojo.com/dojo/json?callback=DojoList

这是活动。

public class GetData extends ActionBarActivity {

private static final String URL = "http://zen.coderdojo.com/dojo/json?callback=DojoList";

GoogleMap map;
HttpClient client;
JSONObject json;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.find_dojo);
    getActionBar().setHomeButtonEnabled(true);
    map = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.gmDojos)).getMap();
    map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    client = new DefaultHttpClient();
    new Read().execute("latitude");
}

public JSONObject mapMarker(String latlongitude)
        throws ClientProtocolException, IOException, JSONException {
    StringBuilder url = new StringBuilder(URL);

    HttpGet get = new HttpGet(url.toString());
    HttpResponse response = client.execute(get);
    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        JSONArray obj = new JSONArray(data);
        JSONObject latlng = obj.getJSONObject(1);
        return latlng;
    } else {
        Toast.makeText(getApplicationContext(), "Error receiving data",
                Toast.LENGTH_SHORT);
        return null;
    }

}

public class Read extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            json = mapMarker("latitude");
            return json.getString(params[1]);
        } catch (IOException | JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
    }
}
}

eclipse中没有出现错误。我在TextView中对此进行了测试,但它没有更改文本,因此我知道它不会检索数据。

我还希望从JSON文件中获取经度并获取纬度和经度的所有实例。任何帮助我如何解决这个问题并获得经度表示赞赏。

0 个答案:

没有答案