由于请求数据接口是跨域的,但是我们无法改变接口的代码

先从请求后台,然后从后台进行二次请求,请求数据接口

原生代码

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接口使用的更多相关文章

  1. 免费股票数据API接口

    免费股票数据API接口提供沪深.香港.美国股市信息. 1.沪深股市 2.香港股市 3.美国股市 4.香港股市列表 5.美国股市列表 6.深圳股市列表 7.沪股列表 API文档:https://www. ...

  2. 文华财经赢顺外盘期货行情数据API接口开放代码

    文华财经赢顺外盘期货行情数据API接口开放代码        怎么才能获取到外盘期货行情数据API接口呢?不少朋友就会考虑到文华财经行情API接口,本身文华财经就是一个软件提供商,提供行情API接口也 ...

  3. 易盛信息9.0外盘期货行情数据API接口公共授权开发包例子代码

    易盛信息9.0外盘期货行情数据API接口公共授权开发包例子代码        怎么才能获取到外盘期货行情数据API接口呢?不少朋友就会考虑到易盛9.0行情API接口,本身易盛就是一个软件提供商,提供行 ...

  4. 数字货币比特币以太坊买卖五档行情数据API接口

    数字货币比特币以太坊买卖五档行情数据API接口       数字货币一般包含比特币BTC.以太坊ETH.瑞波币XRP.泰达币USDT.比特币现金BCH.比特币SV.莱特币LTC.柚子币EOS.OKB. ...

  5. 如何获取东方财富文华新浪财经实时行情数据API接口

    BIGI行情期货外汇股指A股期权实时行情数据文华新浪财经API接口新浪财经并非实时行情数据源,所以获取的行情数据源也并非实时的.以下介绍的方法和新浪财经获取行情数据源的方法是一致的.需要实时行情数据源 ...

  6. 免费的无次数限制的各类API接口(2)

    之前整理过一些聚合数据上的免费API(各类免费的API接口分享,无限次),这次还有一些其他的进行了整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以 ...

  7. 各类无次数限制的免费API接口整理

    各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...

  8. 网络免费API接口整理

    转载自: https://www.cnblogs.com/doit8791/p/9351629.html 从网上看到一些免费API接口,在个人开发小程序等应用练手时可试用. 各类无次数限制的免费API ...

  9. 各类无次数限制的免费API接口,再也不怕找不到免费API了

    各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...

随机推荐

  1. 如何判断ACCESS数据库有无密码

    因为没有密码的数据库即使加上密码选项连接也不报错,所以如果通过连接来判读就无法识别无密码的数据库. 通过设置密码可以来测试数据库是否有密码,这是由于修改数据库密码的前提是数据库必须先有密码才行,如果数 ...

  2. 《Celeste》 开发者是如何精心制作“冲刺”的

    转自:http://www.gameres.com/804804.html 简介与序曲 在Celesete里,许多细微的行动都是发生在转瞬之间的,甚至往往比你想象中还要“转瞬之间”. 这里是 [游戏机 ...

  3. 涂抹mysql笔记-管理mysql库和表

    mysql的表对象是基于库维护的,也就是说它属于某个库,不管对象是由谁创建的,只要库在表就在.这根Oracle不同Oracle中的表对象是基于用户的.属于创建改对象的用户所有,用户在表就在.mysql ...

  4. 第18课 类型萃取(2)_获取返回值类型的traits

    1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...

  5. bitbucket 上公钥SSH key如何add key并进行项目运用

    前提:从sourcetree 添加项目时老是拉取不下来,查到原因是应为bitbucket需要SSH key公钥 目的:公钥相当于你在任何一台电脑只要有公钥授权就可以随时提交代码到服务器 原因: 1.很 ...

  6. 统计uint64的数对应二进制数的1的个数

    // pc[i] is the populatio  count of ivar pc [256]byte //统计出o~255每个数对应二进制上1的个数func init() {    for i ...

  7. 在cxGrid表格中如何获得当前列的字段名

    var GridDBTableView:TcxGridDBTableView; ColIndex:Integer; FieldName:string; begin GridDBTableView := ...

  8. JavaScript: For , For/in , For/of

    For: define: The for statement can customize how many times you want to execute code Grammar: for (c ...

  9. JVM系列1:Java内存区域

    JVM系列主要分享自己都虚拟机的理解,我自学时的知识框架多来源于<深入理解Java虚拟机_JVM高级特性与最佳实践>这本书,感兴趣的朋友可直接去阅读这本书. 本系列暂定有3部分,它们是学习 ...

  10. 4、订单详情 /items/order/detail?orderNo=201903251750380001

    <template> <div class="write"> <div class="adr"> <div class ...