import
java.io.ByteArrayOutputStream;
import
java.io.InputStream;
import
java.net.*;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
org.json.JSONArray;
import
org.json.JSONObject;
import
android.util.Log;
public
class
JSON {
/**
* 获取"数组形式"的JSON数据,
* 数据形式:[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}]
* @param path 网页路径
* @return 返回List
* @throws Exception
*/
public
static
List<Map<String, String>> getJSONArray(String path)
throws
Exception {
String json =
null
;
List<Map<String, String>> list =
new
ArrayList<Map<String, String>>();
Map<String, String> map =
null
;
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5
*
1000
);
conn.setRequestMethod(
"GET"
);
if
(conn.getResponseCode() ==
200
) {
InputStream is = conn.getInputStream();
byte
[] data = readStream(is);
json =
new
String(data);
JSONArray jsonArray =
new
JSONArray(json);
for
(
int
i =
0
; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
int
id = item.getInt(
"id"
);
String name = item.getString(
"name"
);
map =
new
HashMap<String, String>();
map.put(
"id"
, id +
""
);
map.put(
"name"
, name);
list.add(map);
}
}
for
(Map<String, String> list2 : list) {
String id = list2.get(
"id"
);
String name = list2.get(
"name"
);
Log.i(
"abc"
,
"id:"
+ id +
" | name:"
+ name);
}
return
list;
}
/**
* 获取"对象形式"的JSON数据,
* 数据形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小猪"},{"id":2,"name":"小猫"}]}
* @param path 网页路径
* @return 返回List
* @throws Exception
*/
public
static
List<Map<String, String>> getJSONObject(String path)
throws
Exception {
List<Map<String, String>> list =
new
ArrayList<Map<String, String>>();
Map<String, String> map =
null
;
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5
*
1000
);
conn.setRequestMethod(
"GET"
);
if
(conn.getResponseCode() ==
200
) {
InputStream is = conn.getInputStream();
byte
[] data = readStream(is);
String json =
new
String(data);
JSONObject jsonObject=
new
JSONObject(json);
int
total=jsonObject.getInt(
"total"
);
Boolean success=jsonObject.getBoolean(
"success"
);
Log.i(
"abc"
,
"total:"
+ total +
" | success:"
+ success);
JSONArray jsonArray = jsonObject.getJSONArray(
"arrayData"
);
for
(
int
i =
0
; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
int
id = item.getInt(
"id"
);
String name = item.getString(
"name"
);
map =
new
HashMap<String, String>();
map.put(
"id"
, id +
""
);
map.put(
"name"
, name);
list.add(map);
}
}
for
(Map<String, String> list2 : list) {
String id = list2.get(
"id"
);
String name = list2.get(
"name"
);
Log.i(
"abc"
,
"id:"
+ id +
" | name:"
+ name);
}
return
list;
}
/**
* 获取类型复杂的JSON数据
*数据形式:
{"name":"小猪",
"age":23,
"content":{"questionsTotal":2,
"questions": [ { "question": "what's your name?", "answer": "小猪"},{"question": "what's your age", "answer": "23"}]
}
}
* @param path 网页路径
* @return 返回List
* @throws Exception
*/
public
static
List<Map<String, String>> getJSON(String path)
throws
Exception {
List<Map<String, String>> list =
new
ArrayList<Map<String, String>>();
Map<String, String> map =
null
;
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5
*
1000
);
conn.setRequestMethod(
"GET"
);
if
(conn.getResponseCode() ==
200
) {
InputStream is = conn.getInputStream();
byte
[] data = readStream(is);
String json =
new
String(data);
public
static
byte
[] readStream(InputStream inputStream)
throws
Exception {
ByteArrayOutputStream bout =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
int
len =
0
;
while
((len = inputStream.read(buffer)) != -
1
) {
bout.write(buffer,
0
, len);
}
bout.close();
inputStream.close();
return
bout.toByteArray();
}
}