保存进度!

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,85 +22,71 @@ import java.nio.charset.StandardCharsets;
/**
* Http相关的工具类
*/
public class HttpUtils
{
public class HttpUtils {
/**
* 以POST方式发送http请求
* @param url 访问的链接字符串
*
* @param url 访问的链接字符串
* @param headers 请求头部参数集合
* @param params 请求体字符串
* @param params 请求体字符串
*/
public static String postHttpRequest( String url, HashMap<String, String> headers,
String param ) throws MalformedURLException
{
public static String postHttpRequest(String url, HashMap<String, String> headers,
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 );
httpURL = new URL(url);
conn = (HttpURLConnection) httpURL.openConnection();
// 设置请求方式
conn.setRequestMethod( "POST" );
conn.setRequestMethod("POST");
// 设置请求头参数
for ( HashMap.Entry<String, String> head : headers.entrySet())
{
conn.setRequestProperty( head.getKey(), head.getValue() );
for (HashMap.Entry<String, String> head : headers.entrySet()) {
conn.setRequestProperty(head.getKey(), head.getValue());
}
// 连接
conn.setDoInput( true );
conn.setDoOutput( true );
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
// 输出请求
out = new OutputStreamWriter( conn.getOutputStream(), StandardCharsets.UTF_8 );
out.write( param );
out = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
out.write(param);
out.flush();
// 读取返回值
in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = in.readLine();
while (line != null)
{
result.append( line );
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,94 +19,76 @@ 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 )
{
//拼接url字符串
String json = "";
public static String getWeatherDisasterWarningJSON(String queryURL,
String userKey,
String cityCode,
boolean useProxy) {
// 拼接url字符串
String json = "";
String requestURL = queryURL + "key=" + userKey + "&location=" + cityCode;
//链接用
HttpURLConnection connection = null;
URL url = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
StringBuilder buffer = new StringBuilder();
// 链接用
HttpURLConnection connection = null;
URL url = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
StringBuilder buffer = new StringBuilder();
try
{
url = new URL( requestURL );
try {
url = new URL(requestURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod( "GET" );
connection.setConnectTimeout( 15000 );
connection.setReadTimeout( 60000 );
connection.setRequestMethod("GET");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.connect();
//如果responseCode为200说明访问成功
if ( connection.getResponseCode() == 200 )
{
//注意和风使用了gzip压缩响应体
inputStream = new GZIPInputStream( connection.getInputStream() );
bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "UTF-8" ) );
// 如果responseCode为200说明访问成功
if (connection.getResponseCode() == 200) {
// 注意和风使用了gzip压缩响应体
inputStream = new GZIPInputStream(connection.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//读出数据
// 读出数据
String temp = bufferedReader.readLine();
while ( temp != null )
{
buffer.append( temp );
while (temp != null) {
buffer.append(temp);
temp = bufferedReader.readLine();
}
json = buffer.toString();
}
}
catch ( MalformedURLException error )
{
} catch (MalformedURLException error) {
error.printStackTrace();
}
catch ( IOException error )
{
System.out.println( "读取失败!" );
}
finally
{
if ( bufferedReader != null )
{
try
{
} catch (IOException error) {
System.out.println("读取失败!");
} 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,15 +98,15 @@ public class WeatherDisasterWarningGrabber
/**
* 将天气警告的json字符串转换成java对象
*
* @param json json字符串
* @return 返回 QWeatherDisasterWarning 对象
* @return 返回 QWeatherDisasterWarning 对象
* @throws IOException
*/
public static QWeatherDisasterWarning convertWeatherDisasterWarning( String json )
throws IOException
{
public static QWeatherDisasterWarning convertWeatherDisasterWarning(String json)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
QWeatherDisasterWarning warning = mapper.readValue( json, QWeatherDisasterWarning.class );
QWeatherDisasterWarning warning = mapper.readValue(json, QWeatherDisasterWarning.class);
return warning;
}