用maven定制一个车联网的天气预报解析项目

用maven定制一个车联网的天气预报解析项目1:首先我们要根据之前的学习先创建一个maven项目 本实例以Gao这个项目来介绍,我们要做的功能是把车联网返回的内容解析并格式化后显示出来。车联网天气查询api地址

在此我们需要一个开发者密钥即访问接口中的参数ak。我已经申请好,没有直接使用了,没有的童鞋可以去申请一个。

2:添加新的依赖 由于我们要解析接口默认返回的xml格式的数据,所以我们要用到一些解析xml的jar包,本例子中用到了dom4j和jaxen相关的jar包来解析xml所需添加的依赖如下>commons-beanutils> >commons-collections> >commons-lang> >commons-logging> >commons-logging-api> >ezmorph> >jaxen> >json-lib>jdk13>>2.6.0>>2.9.1>>1.2.5>>1.4>>1.6.1>>1.2.17>>1.6.1>>1.6.1>>1.1.23</version> </dependency>3:由于代码比较多,实体类就不展示了,,下面看主体代码 项目主程序MyWeatherpackage import javaimport javaimport javaimport javaimport orgimport import import /** * 主程序 * @author Administrator */public class MyWeather { public static void main(String[] args) throws Exception { // 初始化 Log4J PropertyConfigurator.configure(MyWeather.class.getClassLoader() .getResource(“log4j.properties”));//maven项目的src/main/resources下 BaiduRetriever baidu = new BaiduRetriever(); //随机选取一个城市来查询天气 String city = BaiduRetriever.citys.get(new Random().nextInt(953)).getName(); InputStream ins = baidu.retrieve(city); new MyWeather().start(ins); } public void start(InputStream dataIn) throws Exception { // 解析数据 CityWeatherResponse response = new WeatherParser().parseWether(dataIn); //返回正确的状态码进行解析 if(Constants.ERROR_SUCCESS.equals(response.getError())&&Constants.STATUS_SUCCESS.equals(response.getStatus())){ // 用Velocity格式化输出数据 Map<String, Object> templateParam = new HashMap<String, Object>(); templateParam.put(“curdate”, response.getDate()); templateParam.put(“pm25”, response.getResults().getPm25()); templateParam.put(“currentCity”, response.getResults().getCurrentCity());templateParam.put(“weatherList”, response.getResults().getWeatherData());templateParam.put(“indexList”, response.getResults().getIndexList());VelocityTemplate.parseVMTemplateToFile(“weather.vm”, templateParam);/* String text = VelocityTemplate.parseVMTemplate(“weather.vm”, templateParam); System.out.print(text);*/ }else{ System.err.println(“返回结果中有错误信息”); } }}

然后是访问车联网的封装类,此类中还初始化了中国所有的城市信息,以便主程序中每次随机选择一个城市进行查询

BaiduRetriever

package import javaimport javaimport javaimport javaimport javaimport javaimport javaimport javaimport javaimport javaimport javaimport orgimport import import import import import public class BaiduRetriever extends BaseTest{ private static Logger log = Logger.getLogger(BaiduRetriever.class); public static List<City> citys = new ArrayList<City>(); static{ String path = BaiduRetrieverFileInputStream in =null;BufferedReader br = null; StringBuffer sbuf = new StringBuffer();try{File jsonFile = new File(path); in = new FileInputStream(jsonFile); br = new BufferedReader(new InputStreamReader(in));String temp =null;while((temp=br.readLine())!=null){sbuf.append(temp);}}catch (Exception e) {e.printStackTrace();System.err.println(“异常…”);}finally{try {br();} catch (IOException e) {System.err.println(“输入、输出流关闭异常”);e.printStackTrace();}} try{ citys = FastJson.jsonToList(sbuf.toString(), City.class); }catch (Exception e) { System.out.println(“json转化异常”); } } public InputStream retrieve(String location) throws Exception { log.info(“Retrieving Weather Data”); Map<String,Object> paramMap = new HashMap<String,Object>(); paramMap.put(“ak”, Constants.AK); paramMap.put(“location”, location); return readContentFromGet(null, SomeUtil.exChangeMapToParam(paramMap)); } public static void main(String[] args) throws Exception { System().length); /*System.out.println(citys.size()); for (City city:citys) { System.out.println(city.toString()); } BaiduRetriever baidu = new BaiduRetriever(); //随机选取一个城市来查询天气 baidu.retrieve((citys.get(new Random().nextInt(953))).getName());*/ }}找一个让心里安静和干净的地方,自己变得跟水晶一般透明,

用maven定制一个车联网的天气预报解析项目

相关文章:

你感兴趣的文章:

标签云: