提交小修改!

This commit is contained in:
2022-04-22 23:35:29 +08:00
parent f1c1d5a4a5
commit 9c1f141a00
4 changed files with 104 additions and 84 deletions

View File

@@ -2,11 +2,11 @@
* @Author: Kane
* @Date: 2022-04-22 10:53:49
* @LastEditors: Kane
* @LastEditTime: 2022-04-22 17:20:22
* @LastEditTime: 2022-04-22 23:04:58
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\wechat\officalAccount\sendMessage.java
* @Description: 用来推送公众号消息的程序库。
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
package com.cpic.xim.wechat.officalAccount;
@@ -27,7 +27,7 @@ import com.cpic.xim.notify.disaster.*;
/**
* 推送微信公众号消息。
*
*
* @author Kane
*/
public class sendMessage
@@ -38,12 +38,12 @@ public class sendMessage
* 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》
*
* @param wechatOfficalAccountURL 公众号接口网址。
* @param title 通知的标题。
* @param notifyType 通知类型。
* @param notifyMessage 通知文本内容。
* @param title 通知的标题。
* @param notifyType 通知类型。
* @param notifyMessage 通知文本内容。
*/
public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title,
String notifyType, String notifyMessage )
String notifyType, String notifyMessage )
{
// 设置推送内容
WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();
@@ -55,7 +55,7 @@ public class sendMessage
// 转换成json
ObjectMapper mapper;
String json = null;
String json = null;
try
{
@@ -64,14 +64,14 @@ public class sendMessage
json = mapper.writeValueAsString( param );
if ( json.isEmpty())
if ( json.isEmpty() )
{
}
System.out.println( json );
}
catch (JsonProcessingException error)
catch ( JsonProcessingException error )
{
error.printStackTrace();
@@ -79,16 +79,16 @@ public class sendMessage
}
// 推送数据
URL url;
URL url;
HttpURLConnection connection = null;
StringBuilder result = new StringBuilder();
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
OutputStreamWriter out = null;
BufferedReader in = null;
try
{
url = new URL( wechatOfficalAccountURL );
url = new URL( wechatOfficalAccountURL );
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod( "POST" );
@@ -107,14 +107,14 @@ public class sendMessage
String line = in.readLine();
while (line != null)
while ( line != null )
{
result.append( line );
line = in.readLine();
}
}
catch (Exception error)
catch ( Exception error )
{
error.printStackTrace();
}
@@ -122,22 +122,22 @@ public class sendMessage
{
try
{
if ( in != null)
if ( in != null )
{
in.close();
}
if ( out != null)
if ( out != null )
{
out.close();
}
if ( connection != null)
if ( connection != null )
{
connection.disconnect();
}
}
catch (Exception error)
catch ( Exception error )
{
error.printStackTrace();
}
@@ -147,16 +147,31 @@ public class sendMessage
/**
* 推送天气灾害预警!接口文档参考 彭奕洁 编写《消息发送接口调用文档》
*
* @param officalAccountURL 产险厦门分公司公众号接口网址
* @param warning 灾害警报
* @param warning 灾害警报对象
*/
public static void sendWeatherDisasterWarning( String officalAccountURL,
QWeatherDisasterWarning warning )
QWeatherDisasterWarning warning )
{
Vector<QWeatherDisasterWarningItem> warningItems = warning.getWarning();
// 遍历消息
// 设置请求头参数
HashMap<String, String> headers = new HashMap<String, String>();
headers.put( "accept", "*/*" );
headers.put( "Connection", "Keep-Alive" );
headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
// 遍历消息,将消息推送出去。
for ( QWeatherDisasterWarningItem item : warningItems )
{
// 拼接消息内容
String title = item.getTitle();
String text = item.getText();
String color = warningLevel.keySet().contains( item.getLevel() ) ? "#000000"
: warningLevel.get( item.getLevel() );
}
}
static
@@ -176,44 +191,44 @@ class WechatOfficalAccountMessageParameter
WechatOfficalAccountMessageParameter()
{
tplID = 57;
tplID = 57;
groupID = 1;
}
@JsonProperty( "tplid")
@JsonProperty("tplid")
private int tplID;
@JsonProperty( "groupid")
@JsonProperty("groupid")
private int groupID;
@JsonProperty( "first")
@JsonProperty("first")
private String first;
@JsonProperty( "keyword1")
@JsonProperty("keyword1")
private String keyword1;
@JsonProperty( "keyword1color")
@JsonProperty("keyword1color")
private String keyword1Color;
@JsonProperty( "keyword2")
@JsonProperty("keyword2")
private String keyword2;
@JsonProperty( "keyword2color")
@JsonProperty("keyword2color")
private String keyword2Color;
@JsonProperty( "remark")
@JsonProperty("remark")
private String remark;
@JsonProperty( "remarkcolor")
@JsonProperty("remarkcolor")
private String remarkColor;
@JsonProperty( "url")
@JsonProperty("url")
private String url;
@JsonProperty( "appid")
@JsonProperty("appid")
private String appid;
@JsonProperty( "pagepath")
@JsonProperty("pagepath")
private String pagePath;
public int getTplID()

View File

@@ -4,9 +4,9 @@
* @LastEditors: Kane
* @LastEditTime: 2022-04-22 16:02:05
* @FilePath: \DisasterWarning\src\test\java\com\cpic\xim\wechat\officalAccount\sendMessageTest.java
* @Description:
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
* @Description:
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
package com.cpic.xim.wechat.officalAccount;
@@ -28,7 +28,7 @@ public class sendMessageTest
{
// sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" );
}
catch (Exception error)
catch ( Exception error )
{
fail( "测试失败!" );
}
@@ -42,15 +42,15 @@ public class sendMessageTest
+ "keyword1=东山县气象台发布大雾黄色预警[Ⅲ级/较重]&" + "keyword1color=#ffff00&"
+ "keyword2=东山县气象台2022年04月22日06时12分发布大雾黄色预警信号预计未来12小时内我县、闽南渔场和台湾浅滩渔场将出现能见度小于500米的雾。请注意防范";
// headers.put( "accept", "*/*" );
// headers.put( "Connection", "Keep-Alive" );
headers.put( "accept", "*/*" );
headers.put( "Connection", "Keep-Alive" );
headers.put( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
try
{
HttpUtils.postHttpRequest( url, headers, param );
}
catch (Exception error)
catch ( Exception error )
{
}