完成基本功能,准备加入log4j。

This commit is contained in:
Kane Wang 2022-04-22 16:22:23 +08:00
parent 16c54998de
commit 2237690139
5 changed files with 86 additions and 22 deletions

View File

@ -7,6 +7,10 @@
{ {
"city_name": "厦门", "city_name": "厦门",
"city_code": "101230201" "city_code": "101230201"
},
{
"city_name": "东山",
"city_code": "101230608"
} }
], ],
"notify_stuffs": [ "notify_stuffs": [

View File

@ -1,5 +1,13 @@
/*
* @Author: Kane
* @Date: 2022-04-22 10:53:49
* @LastEditors: Kane
* @LastEditTime: 2022-04-22 16:18:48
* @FilePath: \DisasterWarning\src\main\java\AppMain.java
* @Description: 和风天气预警推送厦门太保公众号主程序
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
import com.cpic.xim.config.City; import com.cpic.xim.config.City;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning; import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber; import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
@ -47,7 +55,7 @@ public class AppMain
// 判断是否有警报 // 判断是否有警报
if ( warning.getWarning().isEmpty() == true) if ( warning.getWarning().isEmpty() == true)
{ {
continue;
} }
} }
catch (IOException error) catch (IOException error)

View File

@ -2,18 +2,19 @@
* @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 11:51:02 * @LastEditTime: 2022-04-22 16:18:17
* @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.
*/ */
package com.cpic.xim.httpUtil; package com.cpic.xim.httpUtil;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.net.URL; import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -24,16 +25,19 @@ import java.nio.charset.StandardCharsets;
public class HttpUtils public class HttpUtils
{ {
/** /**
* * 以POST方式发送http请求
* @param url 访问的链接字符串 * @param url 访问的链接字符串
* @param headers 请求头部参数集合 * @param headers 请求头部参数集合
* @param params 请求体字符串 * @param params 请求体字符串
*/ */
public static void postHttpRequest( String url, HashMap<String, String> headers, String param ) public static String postHttpRequest( String url, HashMap<String, String> headers,
throws MalformedURLException String param ) throws MalformedURLException
{ {
URL httpURL = null; URL httpURL = null;
HttpURLConnection conn = null; HttpURLConnection conn = null;
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try try
{ {
@ -52,18 +56,57 @@ public class HttpUtils
conn.setRequestProperty( head.getKey(), head.getValue() ); conn.setRequestProperty( head.getKey(), head.getValue() );
} }
// 设置请求体 // 连接
conn.setDoInput( true );
conn.setDoOutput( true );
conn.connect();
// 输出请求
out = new OutputStreamWriter( conn.getOutputStream(), StandardCharsets.UTF_8 );
out.write( param );
out.flush();
} // 读取返回值
catch (MalformedURLException error) in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
{
String line = in.readLine();
while (line != null)
{
result.append( line );
line = in.readLine();
}
} }
catch (IOException error) catch (IOException error)
{ {
} }
finally
{
try
{
if ( out != null)
{
out.close();
}
if ( in != null)
{
in.close();
}
if ( conn != null)
{
conn.disconnect();
}
}
catch (Exception error)
{
error.printStackTrace();
}
}
return result.toString();
} }
} }

View File

@ -1,4 +1,13 @@
/*
* @Author: Kane
* @Date: 2022-04-22 10:53:49
* @LastEditors: Kane
* @LastEditTime: 2022-04-22 16:22:12
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
* @Description: 用来推送公众号消息的程序库
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
package com.cpic.xim.wechat.officalAccount; package com.cpic.xim.wechat.officalAccount;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -20,7 +29,6 @@ import java.util.Map;
*/ */
public class sendMessage public class sendMessage
{ {
/** /**
* 向公众号推送通知消息接口文档参考 彭奕洁 编写消息发送接口调用文档 * 向公众号推送通知消息接口文档参考 彭奕洁 编写消息发送接口调用文档
* *
@ -31,7 +39,6 @@ public class sendMessage
*/ */
public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title, public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title,
String notifyType, String notifyMessage ) String notifyType, String notifyMessage )
{ {
// 设置推送内容 // 设置推送内容
WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter(); WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();

View File

@ -2,7 +2,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-04-22 11:41:52 * @LastEditTime: 2022-04-22 16:02:05
* @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java * @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java
* @Description: * @Description:
* *
@ -38,12 +38,13 @@ public class sendMessageTest
public void postNotifyMessage() public void postNotifyMessage()
{ {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
String param = "tplid : 57," + "groupid : 1," + "first : \"亲爱的#realname#,您好!\"," String param = "tplid=57&" + "groupid=1&" + "first=亲爱的#realname#,您好!&"
+ "keyword1 : \"厦门发布暴雨红色预警\"," + "keyword1color : \"#ff0000\"," + "keyword1=东山县气象台发布大雾黄色预警[Ⅲ级/较重]&" + "keyword1color=#ffff00&"
+ "keyword2 : \"厦门市气象台**年**月**日发布暴雨红色预警信号\""; + "keyword2=东山县气象台2022年04月22日06时12分发布大雾黄色预警信号预计未来12小时内我县、闽南渔场和台湾浅滩渔场将出现能见度小于500米的雾。请注意防范";
headers.put( "accept", "*/*" ); // headers.put( "accept", "*/*" );
headers.put( "Connection", "Keep-Alive" ); // headers.put( "Connection", "Keep-Alive" );
headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
try try
{ {
@ -52,5 +53,6 @@ public class sendMessageTest
catch (Exception error) catch (Exception error)
{ {
} }
} }
} }