private String getJsonByNetwork() {
// You can get json by this url
final String url = "http://api.androidhive.info/contacts/";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpClient.execute(httpGet);
inputStream = response.getEntity().getContent();
// Json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String tmp = null;
while ((tmp = reader.readLine()) != null) {
sb.append(tmp);
}
result = sb.toString();
} catch (Exception e) {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException se) {
}
}
return result;
}
// Create a JSONObject JSONObject jsonObject = new JSONObject(result);
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
// To get items from the array
for (int i = 0; i < jsonArray.length(); i ++) {
// TODO:Traverse the jsonarray
}
// Create a JSONObject
JSONObject jsonObject = new JSONObject(result);
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
// To get items from the array
for (int i = 0; i < jsonArray.length(); i++) {
// To get a specific JSONObject
JSONObject oneObject = jsonArray.getJSONObject(i);
}
// Create a JSONObject
JSONObject jsonObject = new JSONObject(result);
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
// To get items from the array
for (int i = 0; i < jsonArray.length(); i++) {
// To get a specific JSONObject
JSONObject oneObject = jsonArray.getJSONObject(i);
// To get a specific string
String id = oneObject.getString("id");
String name = oneObject.getString("name");
Log.e("wzy", "id is:" + id + ", name is " + name);
}
03-05 10:26:08.690: E/wzy(26401): id is:c200, name is Ravi Tamada 03-05 10:26:08.690: E/wzy(26401): id is:c201, name is Johnny Depp 03-05 10:26:08.690: E/wzy(26401): id is:c202, name is Leonardo Dicaprio 03-05 10:26:08.690: E/wzy(26401): id is:c203, name is John Wayne 03-05 10:26:08.691: E/wzy(26401): id is:c204, name is Angelina Jolie 03-05 10:26:08.691: E/wzy(26401): id is:c205, name is Dido 03-05 10:26:08.691: E/wzy(26401): id is:c206, name is Adele 03-05 10:26:08.692: E/wzy(26401): id is:c207, name is Hugh Jackman 03-05 10:26:08.693: E/wzy(26401): id is:c208, name is Will Smith 03-05 10:26:08.693: E/wzy(26401): id is:c209, name is Clint Eastwood 03-05 10:26:08.694: E/wzy(26401): id is:c2010, name is Barack Obama 03-05 10:26:08.694: E/wzy(26401): id is:c2011, name is Kate Winslet 03-05 10:26:08.695: E/wzy(26401): id is:c2012, name is Eminem
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有