方法:
//parm:请求的url链接 返回的是json字符串
public static String getURLContent(String urlStr) {
//请求的url
URL url = null;
//请求的输入流
BufferedReader in = null;
//输入流的缓冲
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
//一行一行进行读入
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
} finally{
try{
if(in!=null) {
in.close(); //关闭流
}
}catch(IOException ex) {
}
}
String result =sb.toString();
return result;
} 使用方法:
String string = StringUtil.getURLContent("http://.....");
//转换成Record
//Record data = new Record().setColumns(FastJson.getJson().parse(string, Map.class));
本文介绍了一种使用Java从指定URL获取JSON格式数据的方法。该方法通过打开URL连接并逐行读取内容来实现。此外,还提供了一个示例说明如何将获取到的数据转换为Record对象。
1586

被折叠的 条评论
为什么被折叠?



