例如网址API的网址为:https://wuliang.art/ncov/doc.html#/default/谣言/getRumorListUsingGET
注意:但是在你获取的URl为:http://wuliang.art/ncov/rumor/getRumorList?page=36
解释:
ncov/rumor/getRumorList:是API的接口名
page:参数
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
比如谣言的类:
private Integer id;
private String dateRumor;
private String explainRumor;
private String author;
private String abstractRumor;
private String title;
private String authordesc;
private String idRumor;
注意:get、set别忘记了
1.接口所返回的数据是:
"code": 200,
"message": "SUCCESS",
"data": [
{
"date": "2020-03-02",
"explain": "确有此事",
"arttype": "normal",
"author": "腾讯新闻旗下专业事实查证平台",
"section": "",
"abstract": "据黄梅县人民政府通报:2月28日上午9:30左右...",
"title": "湖北黄冈村民误服二氧化氯消毒片",
"type": 1,
"coversqual": "https://p.qpic.cn/jiaozhen/0/884fbc1ffdaae1aa29ba3049c261f637/0?imageView2/2/w/300/h/300",
"result": "真",
"cover": "https://p.qpic.cn/jiaozhen/0/884fbc1ffdaae1aa29ba3049c261f637/0?imageView2/2/w/150/h/90",
"iscolled": false,
"videourl": "",
"authordesc": "腾讯新闻旗下专业事实查证平台",
"coverrect": "https://p.qpic.cn/jiaozhen/0/884fbc1ffdaae1aa29ba3049c261f637/0",
"markstyle": "true",
"id": "7bf8bf25a07220ad74d4888acde8389d",
"tag": [
"湖北黄冈",
"消毒片"
]
},
{
"date": "2020-03-02",
"explain": "尚无定论",
"arttype": "normal",
"author": "腾讯旗下专业医学科普平台",
"section": "",
"abstract": "针灸是一种传统医学中的治疗方式。此前针对针灸到底是否有效...",
"title": "李跃华的穴位注射苯酚疗法对新冠肺炎治疗有效",
"type": 1,
"coversqual": "https://p.qpic.cn/jiaozhen/0/f328dfe6fbfab4f704f28ce6d5ea2d0a/0?imageView2/2/w/300/h/300",
"result": "疑",
"cover": "https://p.qpic.cn/jiaozhen/0/f328dfe6fbfab4f704f28ce6d5ea2d0a/0?imageView2/2/w/150/h/90",
"iscolled": false,
"videourl": "",
"authordesc": "腾讯旗下专业医学科普平台",
"coverrect": "https://p.qpic.cn/jiaozhen/0/f328dfe6fbfab4f704f28ce6d5ea2d0a/0",
"markstyle": "doubt",
"id": "8658ac436dabd86be6605e47e7bff3c0",
"tag": [
"针灸",
"新型冠状病毒"
]
}
那么就可以用以下的代码块实现
@RequestMapping("/rumor")
public JSONObject rumor() throws Exception{
CloseableHttpClient client = HttpClients.createDefault();
// for(int i=13;i<37;i++)
String url = "http://wuliang.art/ncov/rumor/getRumorList?page=36";
JSONObject jsonObject = null;
HttpGet post = new HttpGet(url);
try {
post.addHeader("content-type", "text/xml");
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());
jsonObject = JSONObject.parseObject(result);
// JSONObject jsonObject1= (JSONObject) jsonObject.get("data");
List<Map<String,Object>> list = (List<Map<String, Object>>) jsonObject.get("data");
for(Map<String,Object> map:list) {
RumorDo rumorDo = new RumorDo();
rumorDo.setDateRumor((String) map.get("date"));
rumorDo.setExplainRumor((String) map.get("explain"));
rumorDo.setAuthor((String) map.get("author"));
rumorDo.setAbstractRumor((String) map.get("abstract"));
rumorDo.setTitle((String) map.get("title"));
rumorDo.setAuthordesc((String) map.get("authordesc"));
rumorDo.setIdRumor((String) map.get("id"));
rumorService.insert(rumorDo);
}
注意:data不是json数据中的
如果接口所返回的数据为:
{
"code": 200,
"message": "SUCCESS",
"data": {
"docs": [
{
"h5url": "https://h5.baike.qq.com/mobile/article.html?docid=tx20406001v3lknw&adtag=txxw.op.fybox",
"image": "https://baike-med-1256891581.file.myqcloud.com/2020023/dec99f30-5843-11ea-b711-37db0881b500_0.jpg",
"abs": "截至目前,NMPA共批准新冠病毒检测试剂15个,核酸检测试剂10个,抗体检测试剂5个。",
"title": "15种新冠病毒检测获批,它们是如何检出病毒的?",
"author_info": {
"name": "",
"icon": "https://baike-med-1256891581.file.myqcloud.com/yidian/production/common/image/source_wicon.png",
"title": "",
"department": "",
"hospital": "腾讯医典医学团队"
}
},
{
"h5url": "https://h5.baike.qq.com/mobile/article.html?docid=tx204060015beohw&adtag=txxw.op.fybox",
"image": "",
"abs": "WHO将病毒传染给十人以上的患者定义为超级传播者。",
"title": "1个感染10个,“超级传播者”如何防控?",
"author_info": {
"name": "",
"icon": "https://baike-med-1256891581.file.myqcloud.com/yidian/production/common/image/source_wicon.png",
"title": "",
"department": "",
"hospital": "腾讯医典医学团队"
}
}
],
"count": 35
}
}
那么所要获取的数据就要多一个:`
@RequestMapping("/diagnosis")
public JSONObject diagnosis() throws Exception{
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://wuliang.art/ncov/news/getDiagnoseList?count=999";
HttpGet post = new HttpGet(url);
JSONObject jsonObject = null;
try{
post.addHeader("content-type","text/xml");
HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());
jsonObject = JSONObject.parseObject(result);
JSONObject jsonObject1= (JSONObject) jsonObject.get("data");
List<Map<String,Object>> list = (List<Map<String, Object>>) jsonObject1.get("docs");
for(Map<String,Object> map:list){
for(Map.Entry<String,Object> entry:map.entrySet()){
System.out.println("key:"+entry.getKey()+","+"value:"+entry.getValue());
Giagnosis giagnosis = new Giagnosis();
giagnosis.setH5url();
}
DiagnosisDo diagnosisDo =new DiagnosisDo();
diagnosisDo.setH5url((String) map.get("h5url"));
diagnosisDo.setImage((String) map.get("image"));
diagnosisDo.setAbs((String) map.get("abs"));
diagnosisDo.setTitle((String) map.get("title"));
//diagnosisDo.setAuthorInfo((String) map.get("author_info"));
System.err.println(map.get("h5url"));
// diagnosisService.insert(diagnosisDo);
}
}
}catch (Exception e){
throw new RuntimeException(e);
}
return jsonObject;
}
注意:两个代码块的
第一个
jsonObject = JSONObject.parseObject(result);
// JSONObject jsonObject1= (JSONObject) jsonObject.get("data");
List<Map<String,Object>> list = (List<Map<String, Object>>) jsonObject.get("data");
第二个
JSONObject jsonObject1= (JSONObject) jsonObject.get("data");
List<Map<String,Object>> list = (List<Map<String, Object>>) jsonObject1.get("docs");
大家可以根据自己的需求试一试