网络API调用与Json的处理
网络API调用与Json的处理经常听说API调用工程师,可什么是API呢?我自己写的Controller算Api吗?其实API就是接口,你自己写的控制层逻辑肯定也算API,但是通常意义上,我们偶尔会调用网络上的API供自己使用,以降低开发难度。获取网络API这里的方法多种多样,网上也有很多稀奇古怪的API可供调用。这里我选择的是一个天气API(前50次白嫖)https://www.apishop.
·
网络API调用与Json的处理
经常听说API调用工程师,可什么是API呢?我自己写的Controller算Api吗?其实API就是接口,你自己写的控制层逻辑肯定也算API,但是通常意义上,我们偶尔会调用网络上的API供自己使用,以降低开发难度。
获取网络API
这里的方法多种多样,网上也有很多稀奇古怪的API可供调用。这里我选择的是一个天气API(前50次白嫖)
https://www.apishop.net/#/
发送API请求
引入阿里巴巴的FastJson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
引入工具类
package com.zxl.src.Utils;
import com.alibaba.fastjson.JSONObject;
import com.zxl.src.entity.Result;
import com.zxl.src.entity.Weather;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@Component
public class ApiUtil {
/**
* 请求接口
*
* @param method 请求方法
* @param url 请求地址
* @param headers 请求头部
* @param params 请求参数
* @return
*/
//@Autowired
//private WeatherMapper weatherMapper;
public static String proxyToDesURL(String method, String url, Map<String, String> headers,
Map<String, String> params) {
// TODO Auto-generated method stub
try {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
//处理请求头部
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null && !headers.isEmpty()) {
Set<String> set = headers.keySet();
for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); ) {
String key = iterator.next();
String value = headers.get(key);
requestHeaders.add(key, value);
}
}
//处理请求参数
MultiValueMap<String, String> paramList = new LinkedMultiValueMap<String, String>();
if (params != null && !params.isEmpty()) {
if (method.equalsIgnoreCase("GET")) {
url += "?";
Set<String> set = params.keySet();
for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); ) {
String key = iterator.next();
String value = params.get(key);
url += key + "=" + value + "&";
}
url = url.substring(0, url.length() - 1);
} else {
Set<String> set = params.keySet();
for (Iterator<String> iterator = set.iterator(); iterator.hasNext(); ) {
String key = iterator.next();
String value = params.get(key);
paramList.add(key, value);
}
}
}
requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
paramList, requestHeaders);
//处理请求方法
HttpMethod requestType = HttpMethod.GET;
method = method.toUpperCase();
switch (method) {
case "GET":
requestType = HttpMethod.GET;
break;
case "POST":
requestType = HttpMethod.POST;
break;
case "PUT":
requestType = HttpMethod.PUT;
break;
case "DELETE":
requestType = HttpMethod.DELETE;
break;
case "HEAD":
requestType = HttpMethod.HEAD;
break;
case "OPTIONS":
requestType = HttpMethod.OPTIONS;
break;
default:
requestType = HttpMethod.GET;
break;
}
ResponseEntity<String> responseEntity = restTemplate.exchange(url, requestType, requestEntity,
String.class, params);
//获取返回结果
return responseEntity.getBody();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
调用
关键代码
//1,获取到的数据为String格式
String result = apiUtil.proxyToDesURL(requestMethod, url, null, params);
if (result != null) {
//2,将获取到的数据为String转换为Json
JSONObject jsonObject = JSONObject.parseObject(result);
String status_code = jsonObject.getString("statusCode");
if (status_code.equals("000000")) {
//3,状态码为000000, 说明请求成功
System.out.println("请求成功:" + jsonObject.getString("result"));
//4,获取到Json中的Result数据
JSONObject json = JSONObject.parseObject(jsonObject.getString("result"));
//⭐5,将Result数据转换为实体类⭐
Result result1 = JSONObject.toJavaObject(json, Result.class);
System.out.println(result1.getDayList());
//6,一个Result中有多个Weather
for(int i = 0;i<result1.getDayList().size();i++){
Weather weather = result1.getDayList().get(i);
//7,将所有Weather插入到数据库中
weatherMapper.insert(weather);
}
} else {
// 状态码非000000, 说明请求失败
System.out.println("请求失败:" + jsonObject.getString("desc"));
return null;
}
}
处理前
{"area":"武汉","areaCode":"420100","areaid":"101200101","dayList":[{"area":"武汉","night_wind_direction":"东北风","night_air_temperature":"10","night_weather_pic":"http://app1.showapi.com/weather/icon/night/00.png","day_weather_code":"00","night_weather":"晴","night_weather_code":"00","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"22","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"东风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211115"},{"area":"武汉","night_wind_direction":"北风","night_air_temperature":"12","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","day_weather_code":"00","night_weather":"多云","night_weather_code":"01","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"17","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"东北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211116"},{"area":"武汉","night_wind_direction":"西南风","night_air_temperature":"8","night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","day_weather_code":"00","night_weather":"多云","night_weather_code":"01","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"18","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211117"},{"area":"武汉","night_wind_direction":"东南风","night_air_temperature":"10","night_weather_pic":"http://app1.showapi.com/weather/icon/night/00.png","day_weather_code":"00","night_weather":"晴","night_weather_code":"00","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"20","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"南风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211118"},{"area":"武汉","night_wind_direction":"东北风","night_air_temperature":"11","night_weather_pic":"http://app1.showapi.com/weather/icon/night/07.png","day_weather_code":"00","night_weather":"小雨","night_weather_code":"07","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"21","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"东风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211119"},{"area":"武汉","night_wind_direction":"北风","night_air_temperature":"7","night_weather_pic":"http://app1.showapi.com/weather/icon/night/07.png","day_weather_code":"07","night_weather":"小雨","night_weather_code":"07","day_weather":"小雨","day_wind_power":"3-4级","day_air_temperature":"15","day_weather_pic":"http://app1.showapi.com/weather/icon/day/07.png","day_wind_direction":"东北风","areaCode":"420100","areaid":"101200101","night_wind_power":"3-4级","daytime":"20211120"},{"area":"武汉","night_wind_direction":"无持续风向","night_air_temperature":"0","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"02","night_weather":"阴","night_weather_code":"02","day_weather":"阴","day_wind_power":"4-5级","day_air_temperature":"14","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211121"},{"area":"武汉","night_wind_direction":"北风","night_air_temperature":"5","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"02","night_weather":"阴","night_weather_code":"02","day_weather":"阴","day_wind_power":"0-3级","day_air_temperature":"11","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"东北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211122"},{"area":"武汉","night_wind_direction":"南风","night_air_temperature":"3","night_weather_pic":"http://app1.showapi.com/weather/icon/night/00.png","day_weather_code":"02","night_weather":"晴","night_weather_code":"00","day_weather":"阴","day_wind_power":"0-3级","day_air_temperature":"11","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"西南风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211123"},{"area":"武汉","night_wind_direction":"东北风","night_air_temperature":"5","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"00","night_weather":"阴","night_weather_code":"02","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"13","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"南风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211124"},{"area":"武汉","night_wind_direction":"东南风","night_air_temperature":"6","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"02","night_weather":"阴","night_weather_code":"02","day_weather":"阴","day_wind_power":"0-3级","day_air_temperature":"13","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"东北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211125"},{"area":"武汉","night_wind_direction":"北风","night_air_temperature":"7","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"02","night_weather":"阴","night_weather_code":"02","day_weather":"阴","day_wind_power":"0-3级","day_air_temperature":"14","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"东南风","areaCode":"420100","areaid":"101200101","night_wind_power":"3-4级","daytime":"20211126"},{"area":"武汉","night_wind_direction":"北风","night_air_temperature":"4","night_weather_pic":"http://app1.showapi.com/weather/icon/night/00.png","day_weather_code":"02","night_weather":"晴","night_weather_code":"00","day_weather":"阴","day_wind_power":"3-4级","day_air_temperature":"11","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"东北风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211127"},{"area":"武汉","night_wind_direction":"东风","night_air_temperature":"4","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"00","night_weather":"阴","night_weather_code":"02","day_weather":"晴","day_wind_power":"0-3级","day_air_temperature":"11","day_weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","day_wind_direction":"东风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211128"},{"area":"武汉","night_wind_direction":"西北风","night_air_temperature":"5","night_weather_pic":"http://app1.showapi.com/weather/icon/night/02.png","day_weather_code":"02","night_weather":"阴","night_weather_code":"02","day_weather":"阴","day_wind_power":"0-3级","day_air_temperature":"12","day_weather_pic":"http://app1.showapi.com/weather/icon/day/02.png","day_wind_direction":"东风","areaCode":"420100","areaid":"101200101","night_wind_power":"0-3级","daytime":"20211129"}],"ret_code":0}
处理后
[com.zxl.src.entity.Weather@4fe59ebc,
com.zxl.src.entity.Weather@299505ed,
com.zxl.src.entity.Weather@221a9e47,
com.zxl.src.entity.Weather@12771f00,
com.zxl.src.entity.Weather@7104638c,
com.zxl.src.entity.Weather@703faa2c,
com.zxl.src.entity.Weather@6a1eaca,
com.zxl.src.entity.Weather@2c047a01,
com.zxl.src.entity.Weather@7ece640e,
com.zxl.src.entity.Weather@732ddf31,
com.zxl.src.entity.Weather@7ad39c24,
com.zxl.src.entity.Weather@7508350d,
com.zxl.src.entity.Weather@786bc1b6,
com.zxl.src.entity.Weather@65a25ddd,
com.zxl.src.entity.Weather@376cff1]
实体类Result
package com.zxl.src.entity;
import lombok.Data;
import java.util.List;
@Data
public class Result {
private String area;
private String areaCode;
private String areaid;
//Result与Weather的一对多关系
private List<Weather> dayList;
}
实体类Weather
package com.zxl.src.entity;
import lombok.Data;
import java.io.Serializable;
/**
* (Weather)实体类
*
* @author makejava
* @since 2021-11-15 15:15:35
*/
@Data
public class Weather implements Serializable {
private static final long serialVersionUID = 190536807806957640L;
/**
* 地区名称
*/
private String area;
/**
* 地区ID
*/
private String areaid;
/**
* 日平均温度
*/
private String dayAirTemperature;
/**
* 天气状况
*/
private String dayWeather;
/**
* 天气状况编码
*/
private String dayWeatherCode;
/**
* 天气状况示例图片
*/
private String dayWeatherPic;
/**
* 风向
*/
private String dayWindDirection;
/**
* 风力
*/
private String dayWindPower;
/**
* 日期
*/
private String daytime;
/**
* 晚间温度
*/
private String nightAirTemperature;
/**
* 晚间天气状况
*/
private String nightWeather;
/**
* 晚间天气状态编码
*/
private String nightWeatherCode;
/**
* 晚间天气状况示例图
*/
private String nightWeatherPic;
/**
* 晚间风向
*/
private String nightWindDirection;
private Integer id;
}
更多推荐






所有评论(0)