通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包:

json-lib-2.4.jar

ezmorph-1.0.6.jar

commons-beanutils-1.8.3.jar

commons-collections-3.2.1.jar

commons-lang-2.6.jar

commons-logging-1.1.3.jar

源码如下:

 package com.web.test;

 import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map; import net.sf.json.JSONObject; public class WeatherHelper { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println(getWeather("101010100"));//北京的天气信息
System.out.println(getWeather("101010100").get("date").toString());
} /**
* 获得天气信息
* @param cityid 城市ID
* @return 天气信息
* @throws IOException
*/
public static Map<String, String> getWeather(String cityid) throws IOException{
String weather = "";
URLConnection connectionData;
BufferedReader br;// 读取data数据流
StringBuilder sb = null; // 连接中央气象台的API
URL url = new URL("http://m.weather.com.cn/data/" + cityid + ".html");
connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
try {
br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
} catch (SocketTimeoutException e) {
System.out.println("连接超时");
} catch (FileNotFoundException e) {
System.out.println("加载文件出错");
}
weather = sb.toString();
System.out.println(weather);
JSONObject jsonData = JSONObject.fromObject(weather);
JSONObject jsonWeather = jsonData.getJSONObject("weatherinfo");
Map<String, String> weatherInfo = new HashMap<String, String>();
weatherInfo.put("cityId", jsonWeather.getString("cityid").toString());//id
weatherInfo.put("cityName", jsonWeather.getString("city").toString());//名称
weatherInfo.put("date", jsonWeather.getString("date_y").toString());//日期
weatherInfo.put("week", jsonWeather.getString("week").toString());//星期
weatherInfo.put("temp", jsonWeather.getString("temp1").toString());//温度
weatherInfo.put("weather", jsonWeather.getString("weather1").toString());//天气现象
weatherInfo.put("wind", jsonWeather.getString("wind1").toString());//风
weatherInfo.put("fl", jsonWeather.getString("fl1").toString());//风力
weatherInfo.put("fx", jsonWeather.getString("fx1").toString());//风向 return weatherInfo;
} }

java获取天气信息的更多相关文章

  1. Java通过webservice接口获取天气信息

    通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...

  2. java解析xml实例——获取天气信息

    获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...

  3. Android实现自动定位城市并获取天气信息

    定位实现代码: <span style="font-size:14px;">import java.io.IOException; import java.util.L ...

  4. 半吊子学习Swift--天气预报程序-获取天气信息

    昨天申请的彩云天气Api开发者今天上午已审核通过  饭后运动过后就马不停蹄的来测试接口,接口是采用经纬度的方式来获取天气信息,接口地址如下 https://api.caiyunapp.com/v2/ ...

  5. 内网公告牌获取天气信息解决方案(C# WebForm)

    需求:内网公告牌能够正确显示未来三天的天气信息 本文关键字:C#/WebForm/Web定时任务/Ajax跨域 规划: 1.天定时读取百度接口获取天气信息并存储至Txt文档: 2.示牌开启时请求Web ...

  6. C#调用WebService获取天气信息

    概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...

  7. Kettle通过Webservice获取天气信息

      Kettle通过Webservice获取天气信息 需求: 通过kettle工具,通过webservice获取天气信息,写成xml格式文件. 思路: Kettle可通过两种选择获取webservic ...

  8. ajax无刷新获取天气信息

    浏览器由于安全方面的问题,禁止ajax跨域请求其他网站的数据,但是可以再本地的服务器上获取其他服务器的信息,在通过ajax请求本地服务来实现: <?php header("conten ...

  9. Java 反射理解(三)-- Java获取方法信息

    Java 反射理解(三)-- Java获取方法信息 基本的数据类型.void关键字,都存在类类型. 举例如下: public class ClassDemo2 { public static void ...

随机推荐

  1. 动态规划(斜率优化):BZOJ 1010 【HNOI2008】 玩具装箱

    玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8218  Solved: 3233[Submit] Description P 教授要去 ...

  2. centos升级openssh的两种方式

    此文介绍的是服务器在有网络和无网络情况下升级openssh方式. 一.首先介绍一个无网络如何升级: 1.准备相关的包 openssh下载地址:  http://mirror.internode.on. ...

  3. UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)

    UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  4. 第k大值01背包问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 /* 第一行输入t 代表t组测试数据 第二行 输入物品个数 背包容量 要求的第k大值 物品的价值 物品的重 ...

  5. angularJS服务一

    一 认识服务 1.服务这个概念其实并不陌生,在其他语言中如java便有这样的概念,其作用就是对外提供某个特定的功能,如消息服务,文件压缩服务等,是一个独立的模块.ng的服务是一个单例对象或函数,对外提 ...

  6. [置顶] cocos2d-x 植物大战僵尸(4) 帽子僵尸的产生

         大家早上好,趁着阳光美好的时候,我打算写下博客:今天要说的是僵尸的产生了,这块和太阳因子的产生比较相似,大体上的区别在于僵尸的基类这块:我在考虑是详细的写还是大体的写,本着对自己作业的态度和 ...

  7. Lucene为不同字段指定不同分词器(转)

    在lucene使用过程中,如果要对同一IndexWriter中不同 Document,不同Field中使用不同的analyzer,我们该如何实现呢? 通过对<lucene in action&g ...

  8. crowd在更改IP后无法登录的问题

    org.codehaus.xfire.fault.XFireFault: Client with address "192.168.1.222", and hostname &qu ...

  9. Error parsing XML: not well-formed (invalid token) 报错+R文件消失解决的方法

    xml报错: 这个xml文件上右键source ->format 注意:res下的文件名称不能大写 R文件消失: 在攻克了其它问题的情况下(或者其它问题还没解决先凝视掉) 手动删除gen pro ...

  10. mybatis3温故

    MyBatis可以利用SQL映射文件来配置,也可以利用Annotation来设置.MyBatis提供的一些基本注解如下表所示. 注解 目标 相应的XML 描述 @CacheNamespace 类 &l ...