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 ...
随机推荐
- sed删除文本第一个匹配行
源文本如下,要求删除第一个为happy-123456的行. ----------------------------- aaaaaaa happy- bbbbbb asdasawe happy- ds ...
- Linux系统下UDP发送和接收广播消息小例子
// 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...
- wpa_cli 连接 wifi
转自:http://hi.baidu.com/yyangjjun/item/9dfe8e175439fc7a1009b5ba 1: run wpa_supplicant first use the ...
- Linux下的定时器:alarm()与setitimer()
Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...
- SharePoint Server 2007 Enterprise Key
正式版 key SN: Tkjcb-3wkhk-2ty2t-qymk2-9xm2y 这个版本也是通过Key来区分是否是测试版还是正式版的 也就是说你输入正式版的Key他就是正式版,输入Enterpri ...
- USACO月赛数据
终于找到了usaco月赛的数据…… 根据月赛的名称,我们可以写出数据地址.比如08年一月的月赛即是:http://contest.usaco.org/JAN08 这里要注意区分大小写.
- SRM 402(1-250pt, 1-500pt)
DIV1 250pt 题意:对于任意一个由1-n组成的数列,其原始顺序为1,2,3..n.给出1~n的一个排列a[n],要通过swp操作将其变回原始顺序.当i < j且a[i] > a[j ...
- Jenkins 学习资料
学习资料: iTech's Blog: Jenkins 入门总结 爱自己: 18篇博客 阳光温暖了心情: 17 篇博客 官网 参考: 构建基于Jenkins + Github的持续集成环境 CI持续集 ...
- ie8下$(document).on('mouseover mouseout','ul li',function(){})的bug
$(document).on('mouseover mouseout','ul li',function(){ if (event.type == 'mouseover') { c ...
- Demon_背包系统(实现装备栏,背包栏,可以切换装备)
using UnityEngine; using System.Collections; public enum BoxType { Normal,//普通格子 Equip//装备栏格子 } publ ...