综合数据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>
上一篇:listview复用机制研究


下一篇:转】windows下使用批处理脚本实现多个版本的JDK切换