在这里,这将起作用。我使用的是严格策略,但通常你应该使用异步。请在谷歌上搜索为什么我们应该使用 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();
}