保存进度!

This commit is contained in:
Kane Wang 2022-11-04 11:50:52 +08:00
parent 9b0d6a8c6d
commit 7dce36562c
4 changed files with 71 additions and 104 deletions

View File

@ -3,7 +3,7 @@
* @Author: Kane
* @Date: 2022-04-22 10:53:49
* @LastEditors: Kane
* @LastEditTime: 2022-06-07 13:16:35
* @LastEditTime: 2022-11-04 11:50:17
* @FilePath: \DisasterWarning\src\main\java\AppMain.java
* @Description: 和风天气预警推送厦门太保公众号主程序
*
@ -63,7 +63,7 @@ public class AppMain {
for (City city : cities) {
try {
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON(queryURL,
userKey, city.getCityCode());
userKey, city.getCityCode(), false);
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning(json);
logger.log(Level.INFO, "查询{0}天气预警,结果:{1}。", new Object[] { city.getCityName(), json });

View File

@ -2,7 +2,7 @@
* @Author: Kane
* @Date: 2022-04-22 09:54:05
* @LastEditors: Kane
* @LastEditTime: 2022-04-22 16:18:17
* @LastEditTime: 2022-11-04 11:48:28
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\httpUtil\HttpUtils.java
* @Description: http相关的工具类
* Copyright (c) ${2022} by Kane, All Rights Reserved.
@ -22,25 +22,23 @@ import java.nio.charset.StandardCharsets;
/**
* Http相关的工具类
*/
public class HttpUtils
{
public class HttpUtils {
/**
* 以POST方式发送http请求
*
* @param url 访问的链接字符串
* @param headers 请求头部参数集合
* @param params 请求体字符串
*/
public static String postHttpRequest(String url, HashMap<String, String> headers,
String param ) throws MalformedURLException
{
String param) throws MalformedURLException {
URL httpURL = null;
HttpURLConnection conn = null;
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
try {
// 获取connection
httpURL = new URL(url);
conn = (HttpURLConnection) httpURL.openConnection();
@ -49,8 +47,7 @@ public class HttpUtils
conn.setRequestMethod("POST");
// 设置请求头参数
for ( HashMap.Entry<String, String> head : headers.entrySet())
{
for (HashMap.Entry<String, String> head : headers.entrySet()) {
conn.setRequestProperty(head.getKey(), head.getValue());
}
@ -69,38 +66,27 @@ public class HttpUtils
String line = in.readLine();
while (line != null)
{
while (line != null) {
result.append(line);
line = in.readLine();
}
}
catch (IOException error)
{
} catch (IOException error) {
}
finally
{
try
{
if ( out != null)
{
} finally {
try {
if (out != null) {
out.close();
}
if ( in != null)
{
if (in != null) {
in.close();
}
if ( conn != null)
{
if (conn != null) {
conn.disconnect();
}
}
catch (Exception error)
{
} catch (Exception error) {
error.printStackTrace();
}
}

View File

@ -1,7 +1,6 @@
package com.cpic.xim.notify.disaster;
import java.util.Date;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

View File

@ -19,17 +19,17 @@ import java.util.zip.GZIPInputStream;
/**
*
*/
public class WeatherDisasterWarningGrabber
{
public class WeatherDisasterWarningGrabber {
/***
* 从和风天气获取天气警报json字符串
*
* @param cityCode 城市或区域代码
* @return 返回警报的json字符串
*/
public static String getWeatherDisasterWarningJSON(String queryURL,
String userKey,
String cityCode )
{
String cityCode,
boolean useProxy) {
// 拼接url字符串
String json = "";
String requestURL = queryURL + "key=" + userKey + "&location=" + cityCode;
@ -41,8 +41,7 @@ public class WeatherDisasterWarningGrabber
BufferedReader bufferedReader = null;
StringBuilder buffer = new StringBuilder();
try
{
try {
url = new URL(requestURL);
connection = (HttpURLConnection) url.openConnection();
@ -52,8 +51,7 @@ public class WeatherDisasterWarningGrabber
connection.connect();
// 如果responseCode为200说明访问成功
if ( connection.getResponseCode() == 200 )
{
if (connection.getResponseCode() == 200) {
// 注意和风使用了gzip压缩响应体
inputStream = new GZIPInputStream(connection.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
@ -61,8 +59,7 @@ public class WeatherDisasterWarningGrabber
// 读出数据
String temp = bufferedReader.readLine();
while ( temp != null )
{
while (temp != null) {
buffer.append(temp);
temp = bufferedReader.readLine();
@ -70,43 +67,28 @@ public class WeatherDisasterWarningGrabber
json = buffer.toString();
}
}
catch ( MalformedURLException error )
{
} catch (MalformedURLException error) {
error.printStackTrace();
}
catch ( IOException error )
{
} catch (IOException error) {
System.out.println("读取失败!");
}
finally
{
if ( bufferedReader != null )
{
try
{
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
}
catch ( IOException error )
{
} catch (IOException error) {
error.printStackTrace();
}
}
if ( inputStream != null )
{
try
{
if (inputStream != null) {
try {
inputStream.close();
}
catch ( IOException error )
{
} catch (IOException error) {
error.printStackTrace();
}
}
if ( connection != null )
{
if (connection != null) {
connection.disconnect();
}
}
@ -116,13 +98,13 @@ public class WeatherDisasterWarningGrabber
/**
* 将天气警告的json字符串转换成java对象
*
* @param json json字符串
* @return 返回 QWeatherDisasterWarning 对象
* @throws IOException
*/
public static QWeatherDisasterWarning convertWeatherDisasterWarning(String json)
throws IOException
{
throws IOException {
ObjectMapper mapper = new ObjectMapper();
QWeatherDisasterWarning warning = mapper.readValue(json, QWeatherDisasterWarning.class);