java获取天气信息
通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包:
源码如下:
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获取天气信息的更多相关文章
- Java通过webservice接口获取天气信息
通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...
- java解析xml实例——获取天气信息
获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...
- Android实现自动定位城市并获取天气信息
定位实现代码: <span style="font-size:14px;">import java.io.IOException; import java.util.L ...
- 半吊子学习Swift--天气预报程序-获取天气信息
昨天申请的彩云天气Api开发者今天上午已审核通过  饭后运动过后就马不停蹄的来测试接口,接口是采用经纬度的方式来获取天气信息,接口地址如下 https://api.caiyunapp.com/v2/ ...
- 内网公告牌获取天气信息解决方案(C# WebForm)
需求:内网公告牌能够正确显示未来三天的天气信息 本文关键字:C#/WebForm/Web定时任务/Ajax跨域 规划: 1.天定时读取百度接口获取天气信息并存储至Txt文档: 2.示牌开启时请求Web ...
- C#调用WebService获取天气信息
概述 本文使用C#开发Winform应用程序,通过调用<WebXml/>(URL:http://www.webxml.com.cn)的WebService服务WeatherWS来获取天气预 ...
- Kettle通过Webservice获取天气信息
Kettle通过Webservice获取天气信息 需求: 通过kettle工具,通过webservice获取天气信息,写成xml格式文件. 思路: Kettle可通过两种选择获取webservic ...
- ajax无刷新获取天气信息
浏览器由于安全方面的问题,禁止ajax跨域请求其他网站的数据,但是可以再本地的服务器上获取其他服务器的信息,在通过ajax请求本地服务来实现: <?php header("conten ...
- Java 反射理解(三)-- Java获取方法信息
Java 反射理解(三)-- Java获取方法信息 基本的数据类型.void关键字,都存在类类型. 举例如下: public class ClassDemo2 { public static void ...
随机推荐
- 网络流(最大流) CQOI 2015 BZOJ 3931 网络吞吐量
3931: [CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活 动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器 ...
- lightoj 1005 组合数学
题目链接:http://lightoj.com/volume_showproblem.php?problem=1005 #include <cstdio> #include <cst ...
- D - How Many Answers Are Wrong(hdu 3038)
总算碰到一道不那么无聊的题了^^ 先说一下题意吧,有两个人一个叫TT的男孩一个叫FF的女孩(名字太随意了吧....),这个叫TT的男孩会经常叫这个女孩一起玩一个游戏,这个有些是这样的,随便写一个数列, ...
- oracle权限问题
Assign the "Create global objects" user right to the non-Administrator account. 1. Click S ...
- web —— jsp笔记
cookies 的使用 1.首先确保客户机上允许使用cookie,一般在浏览器的 隐私 设置项里可以设置. 2.下面给出具体的例子 a)index.jsp中如果没注册过,让游客注册,如果注册了,拿出贮 ...
- 《开源分享2》:《开源框架实战宝典电子书V1.0.0》完整版!
经过一个多月的整理,<J2EE开源框架实战宝典>--Tiny文档PDF电子书開始发放,共同拥有将近600页.为喜爱Tiny.热爱Java开源框架的朋友提供更加体贴的文档服务! 下载地址:h ...
- Linux下生产者与消费者的线程实现
代码见<现代操作系统> 第3版. 为了显示效果,添加了printf()函数来显示运行效果 #include<stdio.h> #include<pthread.h> ...
- 2013ACM暑假集训总结-致将走上大三征途的我
回想起这个暑假,从开始与雄鹰一起的纠结要不要进集训队,与吉吉博博组队参加地大邀请赛,害怕进不了集训队.当时激励我月份开始接触的,记得当时在弄运动会来着,然后就问了雄鹰一些输入输出的东西,怀着满心的期待 ...
- PHP数据结构预热:PHP的迭代器(转)
迭代器有时又称光标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如list或vector)上遍访的接口,设计人员无需关心容器物件的内容. 各种语言实作Iterator的 ...
- CreateProcess函数具体解释
CreateProcess说明:WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程执行指定的可执行文件. 函数原型:BOOL CreateProcess( ...