我第一次这样做所以需要帮助。我有一个 json 文件 https://api.github.com/gists/public ,我想在用户标签下仅获取其数据“登录”和“ID”。请帮助。
查看示例,我尝试从以下代码中获取数据并将内容放入日志中,但日志内容和 url 中的内容不同。
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("https://api.github.com/gists/public"));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,
"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while (true) {
s = buf.readLine();
if (s == null || s.length() == 0)
break;
sb.append(s);
JSONArray ja = new JSONArray(s);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
//items.add(jo.getString("text"));
Log.v("json", "json object : " + jo.getString("user"));
}
Log.v("json", "json file : " + sb);
}
buf.close();
ips.close();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 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();
} finally {
// any cleanup code...
}