完成json配置文件加载。
This commit is contained in:
@@ -4,41 +4,63 @@
|
||||
* @LastEditors: Kane
|
||||
* @LastEditTime: 2022-03-18 00:37:56
|
||||
* @FilePath: \天气灾害预警\src\main\java\AppMain.java
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
* @Description:
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
import com.cpic.xim.disaster_warning.QWeatherDisasterWarning;
|
||||
import com.cpic.xim.disaster_warning.WeatherDisasterWarningGrabber;
|
||||
|
||||
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.nio.file.FileSystems;
|
||||
//import java.nio.file.Path;
|
||||
import java.util.Vector;
|
||||
|
||||
public class AppMain
|
||||
{
|
||||
private static final String CONFIG_FILE_PATH = "./config.json";
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
String cityCode = "101230201";
|
||||
QWeatherDisasterWarning warning;
|
||||
String json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( cityCode );
|
||||
|
||||
if ( json.isEmpty() == true )
|
||||
{
|
||||
System.out.println( "获取天气信息失败!" );
|
||||
}
|
||||
String cityCode = "101230201";
|
||||
String json;
|
||||
QWeatherDisasterWarning warning = null;
|
||||
WeatherDisasterNotifyConfig config = null;
|
||||
|
||||
//读取配置
|
||||
try
|
||||
{
|
||||
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
|
||||
config = WeatherDisasterNotifyConfig.load( CONFIG_FILE_PATH );
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
error.printStackTrace();
|
||||
System.out.println( "读取配置文件失败!" );
|
||||
System.out.println( error.getMessage() );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// if ( warning.)
|
||||
Vector<City> cities = config.getCities();
|
||||
String queryURL = config.getQueryUrl();
|
||||
String userKey = config.getKey();
|
||||
|
||||
System.out.println( "1111" );
|
||||
for ( City city : cities )
|
||||
{
|
||||
try
|
||||
{
|
||||
json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
|
||||
userKey,
|
||||
city.getCityCode() );
|
||||
warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
System.out.println( "查询" + city.getCityName() + "出现异常!");
|
||||
System.out.println( error.getMessage() );
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
52
code/java/天气灾害预警/src/main/java/com/cpic/xim/config/City.java
Normal file
52
code/java/天气灾害预警/src/main/java/com/cpic/xim/config/City.java
Normal file
@@ -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,118 @@
|
||||
package com.cpic.xim.config;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Vector;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String key;
|
||||
private String queryUrl;
|
||||
private Vector<City> cities;
|
||||
private Vector<CpicxmStuff> notifyStuffs;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package com.cpic.xim.disaster_warning;
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import java.util.Vector;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package com.cpic.xim.disaster_warning;
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import java.util.Objects;
|
||||
|
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Copyright (c) ${2022} by Kane, All Rights Reserved.
|
||||
*/
|
||||
package com.cpic.xim.disaster_warning;
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
//import java.util.Objects;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package com.cpic.xim.disaster_warning;
|
||||
package com.cpic.xim.notify.disaster;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -22,11 +22,13 @@ public class WeatherDisasterWarningGrabber
|
||||
* @param cityCode 城市或区域代码
|
||||
* @return 返回警报的json字符串
|
||||
*/
|
||||
public static String getWeatherDisasterWarningJSON( String cityCode )
|
||||
public static String getWeatherDisasterWarningJSON( String queryURL,
|
||||
String userKey,
|
||||
String cityCode )
|
||||
{
|
||||
//拼接url字符串
|
||||
String json = "";
|
||||
String requestURL = QUERY_URL + "key=" + USER_KEY + "&location=" + cityCode;
|
||||
String requestURL = queryURL + "key=" + userKey + "&location=" + cityCode;
|
||||
|
||||
//链接用
|
||||
HttpURLConnection connection = null;
|
Reference in New Issue
Block a user