尝试用vscode开发!
This commit is contained in:
62
code/java/DisasterWarning/src/main/java/AppMain.java
Normal file
62
code/java/DisasterWarning/src/main/java/AppMain.java
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
|
||||
import com.cpic.xim.config.City;
|
||||
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
|
||||
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
|
||||
import com.cpic.xim.config.WeatherDisasterNotifyConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
public class AppMain
|
||||
{
|
||||
private static final String CONFIG_FILE_PATH = "./config.json";
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
String json;
|
||||
WeatherDisasterNotifyConfig config = null;
|
||||
QWeatherDisasterWarning warning = null;
|
||||
|
||||
// 读取配置
|
||||
try
|
||||
{
|
||||
config = WeatherDisasterNotifyConfig.load( CONFIG_FILE_PATH );
|
||||
}
|
||||
catch (IOException error)
|
||||
{
|
||||
System.out.println( "读取配置文件失败!" );
|
||||
System.out.println( error.getMessage() );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Vector<City> cities = config.getCities();
|
||||
String queryURL = config.getQueryUrl();
|
||||
String userKey = config.getKey();
|
||||
|
||||
// 遍历所有城市,查询是否有警报,有则推送。
|
||||
for ( City city : cities)
|
||||
{
|
||||
try
|
||||
{
|
||||
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
|
||||
userKey, city.getCityCode() );
|
||||
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
|
||||
|
||||
// 判断是否有警报
|
||||
if ( warning.getWarning().isEmpty() == true)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
catch (IOException error)
|
||||
{
|
||||
System.out.println( "查询" + city.getCityName() + "出现异常!" );
|
||||
System.out.println( error.getMessage() );
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package com.cpic.xim.config;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class City
|
||||
{
|
||||
public City() {}
|
||||
|
||||
public String getCityName()
|
||||
{
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName( String cityName )
|
||||
{
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public String getCityCode()
|
||||
{
|
||||
return cityCode;
|
||||
}
|
||||
|
||||
public void setCityCode( String cityCode )
|
||||
{
|
||||
this.cityCode = cityCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o )
|
||||
{
|
||||
if ( this == o )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
City city = (City) o;
|
||||
return cityName.equals( city.cityName ) && cityCode.equals( city.cityCode );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash( cityName, cityCode );
|
||||
}
|
||||
|
||||
private String cityName;
|
||||
private String cityCode;
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package com.cpic.xim.config;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CpicxmStuff
|
||||
{
|
||||
public CpicxmStuff() {};
|
||||
|
||||
public String getStuffName()
|
||||
{
|
||||
return stuffName;
|
||||
}
|
||||
|
||||
public void setStuffName( String stuffName )
|
||||
{
|
||||
this.stuffName = stuffName;
|
||||
}
|
||||
|
||||
public String getMobilePhone()
|
||||
{
|
||||
return mobilePhone;
|
||||
}
|
||||
|
||||
public void setMobilePhone( String mobilePhone )
|
||||
{
|
||||
this.mobilePhone = mobilePhone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o )
|
||||
{
|
||||
if ( this == o )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof CpicxmStuff ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
CpicxmStuff that = (CpicxmStuff) o;
|
||||
return stuffName.equals( that.stuffName ) && mobilePhone.equals( that.mobilePhone );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash( stuffName, mobilePhone );
|
||||
}
|
||||
|
||||
private String stuffName;
|
||||
private String mobilePhone;
|
||||
}
|
@@ -0,0 +1,134 @@
|
||||
package com.cpic.xim.config;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
|
||||
public class WeatherDisasterNotifyConfig
|
||||
{
|
||||
private static final int BUFFER_SIZE = 1024;
|
||||
|
||||
public static WeatherDisasterNotifyConfig load( String filePath ) throws IOException
|
||||
|
||||
{
|
||||
WeatherDisasterNotifyConfig config = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
FileReader configFile = null;
|
||||
StringBuffer json = null;
|
||||
char[] buffer = new char[BUFFER_SIZE];
|
||||
|
||||
// 设置json属性
|
||||
mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );
|
||||
|
||||
try
|
||||
{
|
||||
configFile = new FileReader( filePath );
|
||||
json = new StringBuffer();
|
||||
|
||||
int length = configFile.read( buffer );
|
||||
|
||||
while (length != -1)
|
||||
{
|
||||
json.append( buffer );
|
||||
|
||||
length = configFile.read( buffer );
|
||||
}
|
||||
|
||||
config = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
|
||||
}
|
||||
finally
|
||||
{
|
||||
if ( configFile != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
configFile.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
public WeatherDisasterNotifyConfig()
|
||||
{};
|
||||
|
||||
public String getTitle()
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle( String title )
|
||||
{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getKey()
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey( String key )
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getQueryUrl()
|
||||
{
|
||||
return queryUrl;
|
||||
}
|
||||
|
||||
public void setQueryUrl( String queryUrl )
|
||||
{
|
||||
this.queryUrl = queryUrl;
|
||||
}
|
||||
|
||||
public Vector<City> getCities()
|
||||
{
|
||||
return cities;
|
||||
}
|
||||
|
||||
public void setCities( Vector<City> cities )
|
||||
{
|
||||
this.cities = cities;
|
||||
}
|
||||
|
||||
public Vector<CpicxmStuff> getNotifyStuffs()
|
||||
{
|
||||
return notifyStuffs;
|
||||
}
|
||||
|
||||
public void setNotifyStuffs( Vector<CpicxmStuff> notifyStuffs )
|
||||
{
|
||||
this.notifyStuffs = notifyStuffs;
|
||||
}
|
||||
|
||||
public String getWechatOfficalAccountURL()
|
||||
{
|
||||
return wechatOfficalAccountURL;
|
||||
}
|
||||
|
||||
public void setWechatOfficalAccountURL( String wechatOfficalAccountURL )
|
||||
{
|
||||
this.wechatOfficalAccountURL = wechatOfficalAccountURL;
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String key;
|
||||
private String queryUrl;
|
||||
|
||||
@JsonProperty( "wechat_officalaccount_url")
|
||||
private String wechatOfficalAccountURL;
|
||||
private Vector<City> cities;
|
||||
private Vector<CpicxmStuff> notifyStuffs;
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package com.cpic.xim.config.db;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class DBTable
|
||||
{
|
||||
public DBTable() {}
|
||||
|
||||
public String getTableName()
|
||||
{
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName( String tableName )
|
||||
{
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getTableDescription()
|
||||
{
|
||||
return tableDescription;
|
||||
}
|
||||
|
||||
public void setTableDescription( String tableDescription )
|
||||
{
|
||||
this.tableDescription = tableDescription;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o )
|
||||
{
|
||||
if ( this == o )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof DBTable ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
DBTable dbTable = (DBTable) o;
|
||||
return tableName.equals( dbTable.tableName ) && tableDescription.equals( dbTable.tableDescription );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash( tableName, tableDescription );
|
||||
}
|
||||
|
||||
private String tableName;
|
||||
private String tableDescription;
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
package com.cpic.xim.config.db;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class OracleConfig
|
||||
{
|
||||
private String tnsName;
|
||||
private String ipAddr;
|
||||
private String jdbcURL;
|
||||
private String userName;
|
||||
private String password;
|
||||
private Vector<DBTable> tables;
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @Author: Kane
|
||||
* @Date: 2022-04-22 09:54:05
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-04-22 10:32:28
|
||||
* @FilePath: \DisasterWarning\src\main\java\com\cpic\xim\httpUtil\HttpUtils.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
|
||||
package com.cpic.xim.httpUtil;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class HttpUtils
|
||||
{
|
||||
public void postHttpRequest( String url, Map<String, String> headers,
|
||||
Map<String, String> params )
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class QWeatherDisasterWarning
|
||||
{
|
||||
public QWeatherDisasterWarning( String code,
|
||||
String updateTime,
|
||||
String fxLink,
|
||||
Vector<QWeatherDisasterWarningItem> warning,
|
||||
QWeatherDisasterWarningRefer refer )
|
||||
{
|
||||
this.code = code;
|
||||
this.updateTime = updateTime;
|
||||
this.fxLink = fxLink;
|
||||
this.warning = warning;
|
||||
this.refer = refer;
|
||||
}
|
||||
|
||||
public QWeatherDisasterWarning() {}
|
||||
|
||||
public String getCode()
|
||||
{
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode( String code )
|
||||
{
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getUpdateTime()
|
||||
{
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
public void setUpdateTime( String updateTime )
|
||||
{
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
public String getFxLink()
|
||||
{
|
||||
return fxLink;
|
||||
}
|
||||
|
||||
public void setFxLink( String fxLink )
|
||||
{
|
||||
this.fxLink = fxLink;
|
||||
}
|
||||
|
||||
public Vector<QWeatherDisasterWarningItem> getWarning()
|
||||
{
|
||||
return warning;
|
||||
}
|
||||
|
||||
public void setWarning( Vector<QWeatherDisasterWarningItem> warning )
|
||||
{
|
||||
this.warning = warning;
|
||||
}
|
||||
|
||||
public QWeatherDisasterWarningRefer getRefer()
|
||||
{
|
||||
return refer;
|
||||
}
|
||||
|
||||
public void setRefer( QWeatherDisasterWarningRefer refer )
|
||||
{
|
||||
this.refer = refer;
|
||||
}
|
||||
|
||||
private String code;
|
||||
private String updateTime;
|
||||
private String fxLink;
|
||||
private Vector<QWeatherDisasterWarningItem> warning;
|
||||
private QWeatherDisasterWarningRefer refer;
|
||||
}
|
@@ -0,0 +1,212 @@
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
//import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
public class QWeatherDisasterWarningItem
|
||||
{
|
||||
public QWeatherDisasterWarningItem() {}
|
||||
public QWeatherDisasterWarningItem( String id,
|
||||
String sender,
|
||||
String pubTime,
|
||||
String title,
|
||||
String status,
|
||||
String level,
|
||||
String type,
|
||||
String typeName,
|
||||
String text,
|
||||
String related,
|
||||
String urgency, String certainty )
|
||||
{
|
||||
this.id = id;
|
||||
this.sender = sender;
|
||||
this.pubTime = pubTime;
|
||||
this.title = title;
|
||||
this.status = status;
|
||||
this.level = level;
|
||||
this.type = type;
|
||||
this.typeName = typeName;
|
||||
this.text = text;
|
||||
this.related = related;
|
||||
this.urgency = urgency;
|
||||
this.certainty = certainty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object o )
|
||||
{
|
||||
if ( this == o )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QWeatherDisasterWarningItem that = (QWeatherDisasterWarningItem) o;
|
||||
return id.equals( that.id ) && Objects.equals( sender, that.sender ) && Objects.equals( pubTime,
|
||||
that.pubTime ) && Objects.equals(
|
||||
title,
|
||||
that.title ) && Objects.equals( status, that.status ) && Objects.equals( level,
|
||||
that.level ) && Objects.equals(
|
||||
type,
|
||||
that.type ) && Objects.equals( typeName, that.typeName ) && Objects.equals( text,
|
||||
that.text ) && Objects.equals(
|
||||
related,
|
||||
that.related ) && Objects.equals( urgency, that.urgency ) && Objects.equals( certainty,
|
||||
that.certainty );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash( id,
|
||||
sender,
|
||||
pubTime,
|
||||
title,
|
||||
status,
|
||||
level,
|
||||
type,
|
||||
typeName,
|
||||
text,
|
||||
related,
|
||||
urgency,
|
||||
certainty );
|
||||
}
|
||||
|
||||
public String getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId( String id )
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSender()
|
||||
{
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender( String sender )
|
||||
{
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public String getPubTime()
|
||||
{
|
||||
return pubTime;
|
||||
}
|
||||
|
||||
public void setPubTime( String pubTime )
|
||||
{
|
||||
this.pubTime = pubTime;
|
||||
}
|
||||
|
||||
public String getTitle()
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle( String title )
|
||||
{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getStatus()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus( String status )
|
||||
{
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getLevel()
|
||||
{
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel( String level )
|
||||
{
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType( String type )
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getTypeName()
|
||||
{
|
||||
return typeName;
|
||||
}
|
||||
|
||||
public void setTypeName( String typeName )
|
||||
{
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
public String getText()
|
||||
{
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText( String text )
|
||||
{
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getRelated()
|
||||
{
|
||||
return related;
|
||||
}
|
||||
|
||||
public void setRelated( String related )
|
||||
{
|
||||
this.related = related;
|
||||
}
|
||||
|
||||
public String getUrgency()
|
||||
{
|
||||
return urgency;
|
||||
}
|
||||
|
||||
public void setUrgency( String urgency )
|
||||
{
|
||||
this.urgency = urgency;
|
||||
}
|
||||
|
||||
public String getCertainty()
|
||||
{
|
||||
return certainty;
|
||||
}
|
||||
|
||||
public void setCertainty( String certainty )
|
||||
{
|
||||
this.certainty = certainty;
|
||||
}
|
||||
|
||||
private String id;
|
||||
private String sender;
|
||||
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm")
|
||||
private String pubTime;
|
||||
private String title;
|
||||
private String status;
|
||||
private String level;
|
||||
private String type;
|
||||
private String typeName;
|
||||
private String text;
|
||||
private String related;
|
||||
private String urgency;
|
||||
private String certainty;
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @Author: Kane
|
||||
* @Date: 2022-03-17 21:08:09
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-03-18 00:30:53
|
||||
* @FilePath: \天气灾害预警\src\main\java\com\cpic\xim\disaster_warning\QWeatherDisasterWarningRefer.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
//import java.util.Objects;
|
||||
|
||||
public class QWeatherDisasterWarningRefer
|
||||
{
|
||||
public QWeatherDisasterWarningRefer() {}
|
||||
|
||||
public String[] getSources()
|
||||
{
|
||||
return sources;
|
||||
}
|
||||
|
||||
public void setSources( String[] sources )
|
||||
{
|
||||
this.sources = sources;
|
||||
}
|
||||
|
||||
public String[] getLicense()
|
||||
{
|
||||
return license;
|
||||
}
|
||||
|
||||
public void setLicense( String[] license )
|
||||
{
|
||||
this.license = license;
|
||||
}
|
||||
|
||||
private String[] sources;
|
||||
private String[] license;
|
||||
}
|
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @author 王炜
|
||||
* @version 1.0
|
||||
*/
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class WeatherDisasterWarningGrabber
|
||||
{
|
||||
/***
|
||||
* 从和风天气获取天气警报json字符串
|
||||
* @param cityCode 城市或区域代码
|
||||
* @return 返回警报的json字符串
|
||||
*/
|
||||
public static String getWeatherDisasterWarningJSON( String queryURL,
|
||||
String userKey,
|
||||
String cityCode )
|
||||
{
|
||||
//拼接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();
|
||||
|
||||
try
|
||||
{
|
||||
url = new URL( requestURL );
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
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" ) );
|
||||
|
||||
//读出数据
|
||||
String temp = bufferedReader.readLine();
|
||||
|
||||
while ( temp != null )
|
||||
{
|
||||
buffer.append( temp );
|
||||
|
||||
temp = bufferedReader.readLine();
|
||||
}
|
||||
|
||||
json = buffer.toString();
|
||||
}
|
||||
}
|
||||
catch ( MalformedURLException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
System.out.println( "读取失败!" );
|
||||
}
|
||||
finally
|
||||
{
|
||||
if ( bufferedReader != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
bufferedReader.close();
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if ( inputStream != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
inputStream.close();
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if ( connection != null )
|
||||
{
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将天气警告的json字符串转换成java对象。
|
||||
* @param json json字符串
|
||||
* @return 返回 QWeatherDisasterWarning 对象。
|
||||
* @throws IOException
|
||||
*/
|
||||
public static QWeatherDisasterWarning convertWeatherDisasterWarning( String json )
|
||||
throws IOException
|
||||
{
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
QWeatherDisasterWarning warning = mapper.readValue( json, QWeatherDisasterWarning.class );
|
||||
|
||||
return warning;
|
||||
}
|
||||
}
|
@@ -0,0 +1,311 @@
|
||||
|
||||
package com.cpic.xim.wechat.officalAccount;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 推送微信公众号消息。
|
||||
*
|
||||
* @author Kane
|
||||
*/
|
||||
public class sendMessage
|
||||
{
|
||||
|
||||
/**
|
||||
* 向公众号推送通知消息。接口文档参考 彭奕洁 编写《消息发送接口调用文档》
|
||||
*
|
||||
* @param wechatOfficalAccountURL 公众号接口网址。
|
||||
* @param title 通知的标题。
|
||||
* @param notifyType 通知类型。
|
||||
* @param notifyMessage 通知文本内容。
|
||||
*/
|
||||
public static void postNotifyMessageJSON( String wechatOfficalAccountURL, String title,
|
||||
String notifyType, String notifyMessage )
|
||||
|
||||
{
|
||||
// 设置推送内容
|
||||
WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();
|
||||
|
||||
param.setFirst( title );
|
||||
param.setKeyword1( notifyType );
|
||||
param.setKeyword1Color( "#ff0000" );
|
||||
param.setKeyword2( notifyMessage );
|
||||
|
||||
// 转换成json
|
||||
ObjectMapper mapper;
|
||||
String json = null;
|
||||
|
||||
try
|
||||
{
|
||||
mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
|
||||
|
||||
json = mapper.writeValueAsString( param );
|
||||
|
||||
if ( json.isEmpty())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
System.out.println( json );
|
||||
}
|
||||
catch (JsonProcessingException error)
|
||||
{
|
||||
error.printStackTrace();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 推送数据
|
||||
URL url;
|
||||
HttpURLConnection connection = null;
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
OutputStreamWriter out = null;
|
||||
BufferedReader in = null;
|
||||
|
||||
try
|
||||
{
|
||||
url = new URL( wechatOfficalAccountURL );
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestMethod( "POST" );
|
||||
connection.setRequestProperty( "accept", "*/*" );
|
||||
connection.setRequestProperty( "Connection", "Keep-Alive" );
|
||||
connection.setRequestProperty( "Content-Type", "application/json" );
|
||||
connection.setDoInput( true );
|
||||
connection.setDoOutput( true );
|
||||
connection.connect();
|
||||
|
||||
out = new OutputStreamWriter( connection.getOutputStream(), StandardCharsets.UTF_8 );
|
||||
out.write( json );
|
||||
out.flush();
|
||||
|
||||
in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
|
||||
|
||||
String line = in.readLine();
|
||||
|
||||
while (line != null)
|
||||
{
|
||||
result.append( line );
|
||||
|
||||
line = in.readLine();
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
error.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
if ( in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
|
||||
if ( out != null)
|
||||
{
|
||||
out.close();
|
||||
}
|
||||
|
||||
if ( connection != null)
|
||||
{
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @
|
||||
*/
|
||||
public static void postNotifyMessage( String URL, Map<String, String> header, String param )
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class WechatOfficalAccountMessageParameter
|
||||
{
|
||||
|
||||
WechatOfficalAccountMessageParameter()
|
||||
{
|
||||
tplID = 57;
|
||||
groupID = 1;
|
||||
}
|
||||
|
||||
@JsonProperty( "tplid")
|
||||
private int tplID;
|
||||
|
||||
@JsonProperty( "groupid")
|
||||
private int groupID;
|
||||
|
||||
@JsonProperty( "first")
|
||||
private String first;
|
||||
|
||||
@JsonProperty( "keyword1")
|
||||
private String keyword1;
|
||||
|
||||
@JsonProperty( "keyword1color")
|
||||
private String keyword1Color;
|
||||
|
||||
@JsonProperty( "keyword2")
|
||||
private String keyword2;
|
||||
|
||||
@JsonProperty( "keyword2color")
|
||||
private String keyword2Color;
|
||||
|
||||
@JsonProperty( "remark")
|
||||
private String remark;
|
||||
|
||||
@JsonProperty( "remarkcolor")
|
||||
private String remarkColor;
|
||||
|
||||
@JsonProperty( "url")
|
||||
private String url;
|
||||
|
||||
@JsonProperty( "appid")
|
||||
private String appid;
|
||||
|
||||
@JsonProperty( "pagepath")
|
||||
private String pagePath;
|
||||
|
||||
public int getTplID()
|
||||
{
|
||||
return tplID;
|
||||
}
|
||||
|
||||
public void setTplID( int tplID )
|
||||
{
|
||||
this.tplID = tplID;
|
||||
}
|
||||
|
||||
public int getGroupID()
|
||||
{
|
||||
return groupID;
|
||||
}
|
||||
|
||||
public void setGroupID( int groupID )
|
||||
{
|
||||
this.groupID = groupID;
|
||||
}
|
||||
|
||||
public String getFirst()
|
||||
{
|
||||
return first;
|
||||
}
|
||||
|
||||
public void setFirst( String first )
|
||||
{
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
public String getKeyword1()
|
||||
{
|
||||
return keyword1;
|
||||
}
|
||||
|
||||
public void setKeyword1( String keyword1 )
|
||||
{
|
||||
this.keyword1 = keyword1;
|
||||
}
|
||||
|
||||
public String getKeyword1Color()
|
||||
{
|
||||
return keyword1Color;
|
||||
}
|
||||
|
||||
public void setKeyword1Color( String keyword1Color )
|
||||
{
|
||||
this.keyword1Color = keyword1Color;
|
||||
}
|
||||
|
||||
public String getKeyword2()
|
||||
{
|
||||
return keyword2;
|
||||
}
|
||||
|
||||
public void setKeyword2( String keyword2 )
|
||||
{
|
||||
this.keyword2 = keyword2;
|
||||
}
|
||||
|
||||
public String getKeyword2Color()
|
||||
{
|
||||
return keyword2Color;
|
||||
}
|
||||
|
||||
public void setKeyword2Color( String keyword2Color )
|
||||
{
|
||||
this.keyword2Color = keyword2Color;
|
||||
}
|
||||
|
||||
public String getRemark()
|
||||
{
|
||||
return remark;
|
||||
}
|
||||
|
||||
public void setRemark( String remark )
|
||||
{
|
||||
this.remark = remark;
|
||||
}
|
||||
|
||||
public String getRemarkColor()
|
||||
{
|
||||
return remarkColor;
|
||||
}
|
||||
|
||||
public void setRemarkColor( String remarkColor )
|
||||
{
|
||||
this.remarkColor = remarkColor;
|
||||
}
|
||||
|
||||
public String getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl( String url )
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getAppid()
|
||||
{
|
||||
return appid;
|
||||
}
|
||||
|
||||
public void setAppid( String appid )
|
||||
{
|
||||
this.appid = appid;
|
||||
}
|
||||
|
||||
public String getPagePath()
|
||||
{
|
||||
return pagePath;
|
||||
}
|
||||
|
||||
public void setPagePath( String pagePath )
|
||||
{
|
||||
this.pagePath = pagePath;
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
|
||||
package com.cpic.xim.wechat.officalAccount;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class sendMessageTest
|
||||
{
|
||||
private static String url =
|
||||
"https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage";
|
||||
|
||||
@Test
|
||||
public void postNotifyMessageJson()
|
||||
{
|
||||
try
|
||||
{
|
||||
sendMessage.postNotifyMessageJSON( url, "警报", "警报标题", "警报内容!" );
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
fail( "测试失败!" );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postNotifyMessage()
|
||||
{
|
||||
try
|
||||
{
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user