您的位置:首页 > 编程语言 > Java开发

java-后台调用外部接口 返回data

2017-01-06 09:41 513 查看
public class CallExternalData {
/**
* 调用外部接口,返回json数据 -n
* @param path
* @return json-data
*/
@SuppressWarnings("rawtypes")
public static List getExternalJsonData(String path){
StringBuffer document = null ;
BufferedReader reader = null;
String line = null;
try {
URL url = new URL(path);
document = new StringBuffer();
URLConnection conn = url.openConnection();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = reader.readLine()) != null) {
document.append(line);
}
} catch (Exception e) {
throw new RuntimeException("获取外部数据失败,原因:" + e.getMessage());
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONObject json = JSONObject.parseObject(String.valueOf(document));
return Arrays.asList(json.get("data"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: