0

我刚刚开始从 JSON OpenData 获取数据并通过手机进行可视化的主题。

我按照本教程进行操作,一切正常:)

https://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html 我得到数据的网址是:

http:// + api.learn2crack.com/android/json/ (对不起,我的名声不好:))

然后我想尝试一个 Opendata 我和我的 android 应用程序停止,网址是:

http://ckan.opendata.nets.upf.edu/storage/f/2013-11-30T16%3A49%3A59.118Z/london.json

你可以看到它是一样的,我只是在代码中更改了 URL 的名称。你知道问题是不是因为 OpenData?我需要一些许可吗?因为当我执行第二部分时,我的应用程序停止了

4

1 回答 1

0

在这里,这将起作用。我使用的是严格策略,但通常你应该使用异步。请在谷歌上搜索为什么我们应该使用 Async 而不是 Strict Policy。这在这里无关紧要

当我使用 HttpPost 从 url 获取您的 json 时,我收到这些错误:-

405 方法不允许

此资源不允许使用 POST 方法。

您不能发布文件

所以我正在使用 HttpGet :-

        String url = "http://ckan.opendata.nets.upf.edu/storage/f/2013-11-30T16:49:59.118Z/london.json";

    try{
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        //HttpPost httpPost = new HttpPost(url);
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
 BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        JSONObject jObj = new JSONObject(sb.toString());
        JSONArray json2 = json.getJSONArray("user");

        for (int i = 0; i < json2.length(); i++) {
           JSONObject c = json2.getJSONObject(i);
    }
}
catch (Exception e) {
            e.printStackTrace();
        }
于 2013-11-30T17:48:12.000 回答