综合数据api接口使用
由于请求数据接口是跨域的,但是我们无法改变接口的代码
先从请求后台,然后从后台进行二次请求,请求数据接口
原生代码
package edu.nf.http.test; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test; import java.io.IOException; /**
* @Author Eric
* @Date 2018/12/10
*/
public class HttpClientTest {
@Test
public void testGet(){
//创建一个HttpClient对象,用于发送请求和接收响应信息
CloseableHttpClient client = HttpClients.createDefault();
//创建请求构建起(工厂)
RequestBuilder builder = RequestBuilder.get();
//设置请求的uri
builder.setUri("http://c.3g.163.com/nc/article/list/T1348647853363/0-40.html");
//创建一个get请求对象
HttpUriRequest request = builder.build();
//发送请求,并返回响应对象
try {
CloseableHttpResponse response = client.execute(request);
//从响应对象中获取数据,这里使用了一个HttpEntity封装
HttpEntity entity = response.getEntity();
//从Entity中获取真正的响应内容
String json = EntityUtils.toString(entity);
System.out.println(json);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
@Test
public void testPost(){
//创建一个HttpClient对象,用于发送请求和接收响应信息
CloseableHttpClient client = HttpClients.createDefault();
RequestBuilder builder = RequestBuilder.post();
builder.setUri("http://c.3g.163.com/nc/article/list/T1348647853363/0-40.html");
//可以提交参数
builder.addParameter("user","admin").addParameter("age","18");
//构建请求对象
HttpUriRequest request = builder.build();
//发送请求
try {
CloseableHttpResponse response = client.execute(request);
//获取响应数据
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
System.out.println(json);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
封装成工具类
package edu.nf.http.conmmons; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.util.Map; /**
* @Author Eric
* @Date 2018/12/10
* 封装工具类
*/
public class HttpUtils {
/**
* 创建一个唯一的client对象
*/
private static CloseableHttpClient client = HttpClients.createDefault(); /**
* 发送get请求
* @param uri
* @return
*/
public static String get(String uri){
//构建get请求
HttpUriRequest request = RequestBuilder.get(uri).build();
return execute(request);
} /**
* 发送post请求
* @param uri
* @param params post所提交的参数
* @return
*/
public static String post(String uri, Map<String,String>params){
RequestBuilder builder = RequestBuilder.post(uri);
//循环设置参数
for (String name : params.keySet()) {
builder.addParameter(name,params.get(name));
}
HttpUriRequest request = builder.build();
return execute(request);
} /**
* 发送请求并返回响应内容
* @param request
* @return
*/
private static String execute(HttpUriRequest request){
try {
//发送请求
CloseableHttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
json序列化工具类
(获取到的数据是字符串,序列化成json)
package edu.nf.http.conmmons; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; /**
* @Author Eric
* @Date 2018/12/10
*/
public class JsonUtils {
public static<T> T convert(String json,Class<T> clazz){
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json,clazz);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
Service过滤
package edu.nf.http.service; import edu.nf.http.conmmons.HttpUtils;
import edu.nf.http.conmmons.JsonUtils;
import org.springframework.stereotype.Service; import java.util.List;
import java.util.Map; /**
* @Author Eric
* @Date 2018/12/10
*/
@Service
public class NewsService {
public List<Map<String,Object>> listNews(){
String json = HttpUtils.get("http://c.3g.163.com/nc/article/list/T1348647853363/0-40.html");
//将json转换成Map对象
Map<String,List<Map<String,Object>>> map = JsonUtils.convert(json,Map.class);
//进行数据清洗(过滤)
List<Map<String,Object>>list = map.get("T1348647853363");
//删除头行没用的数据
list.remove(0);
return list;
} public static void main(String[] args) {
List<Map<String,Object>>list = new NewsService().listNews();
System.out.println(list.size());
}
}
ResponseVO
package edu.nf.http.controller.vo; /**
* @Author Eric
* @Date 2018/12/6
*/
public class ResponseVO<T> {
private Integer code;
private String message;
private T data; @Override
public String toString() {
return "ResponseVO{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public T getData() {
return data;
} public void setData(T data) {
this.data = data;
}
}
Controller
package edu.nf.http.controller; import edu.nf.http.controller.vo.ResponseVO;
import edu.nf.http.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
import java.util.Map; /**
* @Author Eric
* @Date 2018/12/10
*/
@RestController
public class NewsController {
@Autowired
private NewsService newsService;
@GetMapping("/listNews")
public ResponseVO listNews(){
ResponseVO vo = new ResponseVO();
List<Map<String,Object>> list = newsService.listNews();
vo.setCode(HttpStatus.OK.value());
vo.setMessage("无数据");
vo.setData(list);
return vo;
}
}
HTML
(使用Postman工具进行请求可以查看返回的字段,在页面可以用对象进行输出)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻头条</title>
</head>
<body>
<header class="bar bar-nav">
<h1 class="title">今日头条</h1>
</header>
<div class="content">
<div class="content-block-title">简单列表</div>
<div class="list-block media-list">
<ul id="news_id">
<li>
<div class="item-content">
<div class="item-media"><img src="http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg" style='width: 2.2rem;'></div>
<div class="item-inner">
<div class="item-title-row">
<div class="item-title">标题</div>
</div>
<div class="item-subtitle">子标题</div>
</div>
</div>
</li>
</ul>
</div>
</div>
<link rel="stylesheet" href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
<script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
<script>
$(function(){
$.ajax({
type:"get",
url:"http://localhost:8080/listNews",
success:function(result){
$.each(result.data, function(index,news) {
$("#news_id").append(
"<li>"+
"<div class='item-content'>"+
"<div class='item-media'><img src='"+news.imgsrc+"' style='width: 2.2rem;'></div>"+
"<div class='item-inner'>"+
"<div class='item-title-row'>"+
"<div class='item-title'>"+news.source+"</div>"+
"</div>"+
"<div class='item-subtitle'>"+news.title+"</div>"+
"</div>"+
"</div>"+
"</li>"
);
});
} });
});
</script>
</body>
</html>
综合数据api接口使用的更多相关文章
- 免费股票数据API接口
免费股票数据API接口提供沪深.香港.美国股市信息. 1.沪深股市 2.香港股市 3.美国股市 4.香港股市列表 5.美国股市列表 6.深圳股市列表 7.沪股列表 API文档:https://www. ...
- 文华财经赢顺外盘期货行情数据API接口开放代码
文华财经赢顺外盘期货行情数据API接口开放代码 怎么才能获取到外盘期货行情数据API接口呢?不少朋友就会考虑到文华财经行情API接口,本身文华财经就是一个软件提供商,提供行情API接口也 ...
- 易盛信息9.0外盘期货行情数据API接口公共授权开发包例子代码
易盛信息9.0外盘期货行情数据API接口公共授权开发包例子代码 怎么才能获取到外盘期货行情数据API接口呢?不少朋友就会考虑到易盛9.0行情API接口,本身易盛就是一个软件提供商,提供行 ...
- 数字货币比特币以太坊买卖五档行情数据API接口
数字货币比特币以太坊买卖五档行情数据API接口 数字货币一般包含比特币BTC.以太坊ETH.瑞波币XRP.泰达币USDT.比特币现金BCH.比特币SV.莱特币LTC.柚子币EOS.OKB. ...
- 如何获取东方财富文华新浪财经实时行情数据API接口
BIGI行情期货外汇股指A股期权实时行情数据文华新浪财经API接口新浪财经并非实时行情数据源,所以获取的行情数据源也并非实时的.以下介绍的方法和新浪财经获取行情数据源的方法是一致的.需要实时行情数据源 ...
- 免费的无次数限制的各类API接口(2)
之前整理过一些聚合数据上的免费API(各类免费的API接口分享,无限次),这次还有一些其他的进行了整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以 ...
- 各类无次数限制的免费API接口整理
各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...
- 网络免费API接口整理
转载自: https://www.cnblogs.com/doit8791/p/9351629.html 从网上看到一些免费API接口,在个人开发小程序等应用练手时可试用. 各类无次数限制的免费API ...
- 各类无次数限制的免费API接口,再也不怕找不到免费API了
各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...
随机推荐
- SQLServer 清空某个库所有表
select @n=1 insert #temp(tablename) SELECT distinct sobjects.name FROM sysobjects sobjects WHERE sob ...
- redis排序
1.sort 排序最常见的是sort命令,可以对列表或者有序集合排序,最简单的排序方式如下: > lpush list (integer) > lpush list (integer) & ...
- 多端统一框架尝试--Taro
参考资料 Taro官网Taro GitHubTaro资源汇总Taro-UI 我的demo代码 github地址 Taro介绍和尝试心得 Taro是基于React语法规范开发的多端统一的框架,一套代码可 ...
- maven的tomcat插件问题
在dependence中不用加tomcat的jar, 记得在plugin中加入tomcat插件就行. 否则会出问题.
- sass的基本使用
使用sass的前提是安装Ruby,如果是Mac系统,那么免去安装,Windows系统需要自行安装https://www.sass.hk/install/.当安装好以后,直接执行安装sass命令:gem ...
- css:清楚html所有标签自带属性
相信如果您动手写过网页的话,应该体会到有些标签会自带一些默认的样式,而这些样式或许又是我们不想要的,所以我们可以用以下代码清除所有标签的默认样式 html, body, div, span, ap ...
- 双网卡Iptables端口转发
当前场景如下 跳板机A是双网卡,有一个内网IP和一个公网IP 内网IP: 10.0.10.30 外网IP: 58.68.255.123 内网机器:10.0.30.88,可以和10.0.10.30通讯 ...
- linux下启动tomcat服务
Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/local ...
- 本学期c#学习总结
本学期c#学习总结 时间转瞬即逝,大一上半学期的学习生涯已经结束.虽然以前我没什么关于学习计算机的基础,但是经过了这几个月的学习我也还是有点收获的. 我发现c#语言的关键词有很多语言特性和固定的用法, ...
- step_by_step_webapi执行时间
做开发没多久,这次单位让我做对TB 的机票运价直连接口,其实主要是去sabre gds带上相应的参数去做查询,验仓,下单操作,这次用到asp.net boilerplate 项目模板搭建,用它的动态w ...