有获取灾害信息提醒的需求,但是发现并没有开发的API 。已有的API都需要付钱。 天气预报API,可以看知乎的这边文章。网上的天气 API 哪一个更加可靠? 里面的普通的天气预报部分免费,但是灾害预警的API都是需要收费的。于是自己去摸索了。

最后通过发F12发现了中国天气网中可用的请求: http://www.weather.com.cn/data/alarm_xml/alarminfo.xml

顺便写了下解析使用工具,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@Data
@XmlRootElement(name = "AlermInfo")
public class AlarmInfo {
private AlarmDetail roadIcing;
private AlarmDetail snowStorm;
private AlarmDetail rainStorm;
private AlarmDetail hail;
private AlarmDetail gale;
private AlarmDetail heavyFog;
private AlarmDetail heatWave;
private AlarmDetail drought;
private AlarmDetail coldWave;
private AlarmDetail sWHazards;
private AlarmDetail lightning;
private AlarmDetail haze;
private AlarmDetail sandStorm;
private AlarmDetail frost;
private AlarmDetail typhoon;
private AlarmDetail other;
}

@Data
public class AlarmDetail {
List<Station> station;
}

@Data
public class Station {
@JacksonXmlProperty(isAttribute = true, localName = "stationId")
private String stationId;
@JacksonXmlProperty(isAttribute = true, localName = "areaId")
private String areaId;
@JacksonXmlProperty(isAttribute = true, localName = "stationName")
private String stationName;
@JacksonXmlProperty(isAttribute = true, localName = "lon")
private String lon;
@JacksonXmlProperty(isAttribute = true, localName = "lat")
private String lat;
@JacksonXmlProperty(isAttribute = true, localName = "signalType")
private String signalType;
@JacksonXmlProperty(isAttribute = true, localName = "signalLevel")
private String signalLevel;
@JacksonXmlProperty(isAttribute = true, localName = "issueTime")
private String issueTime;
@JacksonXmlProperty(isAttribute = true, localName = "relieveTime")
private String relieveTime;
@JacksonXmlProperty(isAttribute = true, localName = "issueContent")
private String issueContent;

}


public class JacksonSerialize implements SerializeDelegate {
private static final Logger LOGGER = LoggerFactory.getLogger(JacksonSerialize.class);
private static final ObjectMapper OBJECT_MAPPER;
private static final XmlMapper XML_MAPPER;

static {
OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);

JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XML_MAPPER = new XmlMapper(module);
XML_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
XML_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
XML_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
XML_MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
}

@Override
public <T> T jsonToBean(String json, Class<T> clazz) {
T result = null;
try {
result = OBJECT_MAPPER.readValue(new ByteArrayInputStream(json.getBytes(Const.Charset.UTF_8)), clazz);
} catch (IOException e) {
LOGGER.error("Convert json to bean " + clazz.getName() + " error.", e);
}
return result;
}

@Override
public String beanToJson(Object object) {
String result = null;
try {
result = OBJECT_MAPPER.writeValueAsString(object);
} catch (Exception e) {
LOGGER.error("Convert bean" + object.getClass().getName() + " to json error.", e);
}
return result;
}

@Override
public <T> T xmlToBean(String xml, Class<T> c) {
T t = null;
try {
t = XML_MAPPER.readValue(new ByteArrayInputStream(xml.getBytes(Const.Charset.UTF_8)), c);
} catch (IOException e) {
LOGGER.error("Convert xml to bean " + c.getName() + " error.", e);
}
return t;
}

@Override
public String beanToXml(Object obj) {
String result = null;
try {
result = XML_MAPPER.writeValueAsString(obj);
} catch (Exception e) {
LOGGER.error("Convert bean" + obj.getClass().getName() + " to xml error.", e);
}

return result;
}
}