保存进度!

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

View File

@ -2,7 +2,7 @@
* @Author: Kane * @Author: Kane
* @Date: 2022-04-22 09:54:05 * @Date: 2022-04-22 09:54:05
* @LastEditors: Kane * @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 * @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\httpUtil\HttpUtils.java
* @Description: http相关的工具类 * @Description: http相关的工具类
* Copyright (c) ${2022} by Kane, All Rights Reserved. * Copyright (c) ${2022} by Kane, All Rights Reserved.
@ -22,85 +22,71 @@ import java.nio.charset.StandardCharsets;
/** /**
* Http相关的工具类 * Http相关的工具类
*/ */
public class HttpUtils public class HttpUtils {
{
/** /**
* 以POST方式发送http请求 * 以POST方式发送http请求
* @param url 访问的链接字符串 *
* @param url 访问的链接字符串
* @param headers 请求头部参数集合 * @param headers 请求头部参数集合
* @param params 请求体字符串 * @param params 请求体字符串
*/ */
public static String postHttpRequest( String url, HashMap<String, String> headers, public static String postHttpRequest(String url, HashMap<String, String> headers,
String param ) throws MalformedURLException String param) throws MalformedURLException {
{
URL httpURL = null; URL httpURL = null;
HttpURLConnection conn = null; HttpURLConnection conn = null;
OutputStreamWriter out = null; OutputStreamWriter out = null;
BufferedReader in = null; BufferedReader in = null;
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
try try {
{
// 获取connection // 获取connection
httpURL = new URL( url ); httpURL = new URL(url);
conn = (HttpURLConnection) httpURL.openConnection(); conn = (HttpURLConnection) httpURL.openConnection();
// 设置请求方式 // 设置请求方式
conn.setRequestMethod( "POST" ); 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());
conn.setRequestProperty( head.getKey(), head.getValue() );
} }
// 连接 // 连接
conn.setDoInput( true ); conn.setDoInput(true);
conn.setDoOutput( true ); conn.setDoOutput(true);
conn.connect(); conn.connect();
// 输出请求 // 输出请求
out = new OutputStreamWriter( conn.getOutputStream(), StandardCharsets.UTF_8 ); out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
out.write( param ); out.write(param);
out.flush(); out.flush();
// 读取返回值 // 读取返回值
in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = in.readLine(); String line = in.readLine();
while (line != null) while (line != null) {
{ result.append(line);
result.append( line );
line = in.readLine(); line = in.readLine();
} }
} } catch (IOException error) {
catch (IOException error)
{
} } finally {
finally try {
{ if (out != null) {
try
{
if ( out != null)
{
out.close(); out.close();
} }
if ( in != null) if (in != null) {
{
in.close(); in.close();
} }
if ( conn != null) if (conn != null) {
{
conn.disconnect(); conn.disconnect();
} }
} } catch (Exception error) {
catch (Exception error)
{
error.printStackTrace(); error.printStackTrace();
} }
} }

View File

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

View File

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