不废话直接上代码

public class Main {
static String urlString = "http://www.scggzy.gov.cn/Info/GetInfoListNew?keywords=&times=4&timesStart=&timesEnd=&province=&area=&businessType=&informationType=&industryType=&page=1&parm=1534929604640";
static String page="&page=";
@SuppressWarnings("static-access")
public static void main(String[] args)throws Exception{
// TODO Auto-generated method stub
open_url_test oUrl = new open_url_test();
if (oUrl.openurl(urlString)) {
readData rData = new readData();
JSONObject json = rData.readData(urlString);
JSONObject ob=JSONObject.fromObject(json);
String data= (String) ob.get("data");
data=data.substring(1,data.length()-1);
JSONArray json2=JSONArray.fromObject(data);
for (int i = 0; i < 10; i++) {
JSONObject jsonObject = (JSONObject) json2.get(i);
System.out.println("---------------------------");
System.out.println(jsonObject.get("Title"));
System.out.println(jsonObject.get("CreateDateStr"));
System.out.println(jsonObject.get("TableName"));
System.out.println(jsonObject.get("Link"));
System.out.println(jsonObject.get("username"));
System.out.println(jsonObject.get("province"));
System.out.println(jsonObject.get("businessType"));
System.out.println(jsonObject.get("NoticeType"));
}

}else{

}
}
}

-----------------------------------------------------------------------------------

public class open_url_test {
public static Logger logger = Logger.getLogger(open_url_test.class);
public boolean openurl(String url_infor) throws Exception{
URL url = new URL(url_infor);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设定请求的方法,默认是GET(对于知识库的附件服务器必须是GET,如果是POST会返回405。流程附件迁移功能里面必须是POST,有所区分。)
httpURLConnection.setRequestMethod("GET");
// 设置字符编码
// httpURLConnection.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
int code = httpURLConnection.getResponseCode();
try {
InputStream inputStream = httpURLConnection.getInputStream();
System.out.println("连接成功");
logger.info("打开"+url_infor+"成功!");
return true;
}catch (Exception exception){
logger.info("打开"+url_infor+"失败!");
return false;
}
}
}

----------------------------------------------------------------------------------------------

public class readData {
public static JSONObject readData(String urlString) throws IOException, JSONException{
InputStream is = new URL(urlString).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
String jsonText = sb.toString();
JSONObject json = JSONObject.fromObject(jsonText);
return json;
} finally {
is.close();
}
}
}

----------------------------------------------------------

 结果:

posted on 2018-08-23 11:13  老-.-猫  阅读(529)  评论(0编辑  收藏  举报