java ip调天气预报接口_JAVA + WeatherWebService 实现天气预报接口调取
两步完成接口调取
第一步:引入jar包
若在线jar包失效,私信我即可。
第二步:创建类实现
在JAVA项目中创建java类(我的是:WeatherUtil)
修改类中cityid为你需要展示的地区id,我的是上海的【cityid查询】
WeatherUtil类代码:
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* java调用中央气象局天气预报接口
*
* @author Administrator
*
*/
public class WeatherUtil {
/**
* 获取一周天气
* 方 法 名:getWeekWeatherMap
* @param Cityid 城市编码
*/
public static List> getWeekWeatherMap(String Cityid)
throws IOException, NullPointerException {
// 连接中央气象台的API
URL url = new URL("http://m.weather.com.cn/data/" + Cityid + ".html");
URLConnection connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
// 得到1到6天的天气情况
List> list = new ArrayList>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
connectionData.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
String datas = sb.toString();
System.out.println(datas);
JSONObject jsonData = JSONObject.fromObject(datas);
JSONObject info = jsonData.getJSONObject("weatherinfo");
for (int i = 1; i <= 6; i++) {
// 得到未来6天的日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, i - 1);
Date date = cal.getTime();
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
Map map = new HashMap();
map.put("city", info.getString("city").toString());// 城市
map.put("date_y", sf.format(date));// 日期
map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));// 星期
map.put("fchh", info.getString("fchh").toString());// 发布时间
map.put("weather", info.getString("weather" + i).toString());// 天气
map.put("temp", info.getString("temp" + i).toString());// 温度
map.put("wind", info.getString("wind" + i).toString());// 风况
map.put("fl", info.getString("fl" + i).toString());// 风速
// map.put("index", info.getString("index").toString());//
// 今天的穿衣指数
// map.put("index_uv", info.getString("index_uv").toString());//
// 紫外指数
// map.put("index_tr", info.getString("index_tr").toString());//
// 旅游指数
// map.put("index_co", info.getString("index_co").toString());//
// 舒适指数
// map.put("index_cl", info.getString("index_cl").toString());//
// 晨练指数
// map.put("index_xc", info.getString("index_xc").toString());//
// 洗车指数
// map.put("index_d", info.getString("index_d").toString());//
// 天气详细穿衣指数
list.add(map);
}
} catch (SocketTimeoutException e) {
System.out.println("连接超时");
} catch (FileNotFoundException e) {
System.out.println("加载文件出错");
}
return list;
}
/**
*
* 获取实时天气1
* 方 法 名: getTodayWeather
*
* @param Cityid
* 城市编码
*/
public static Map getTodayWeather1(String Cityid)
throws IOException, NullPointerException {
// 连接中央气象台的API
URL url = new URL("http://www.weather.com.cn/data/sk/" + Cityid
+ ".html");
URLConnection connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
Map map = new HashMap();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
connectionData.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
String datas = sb.toString();
System.out.println(datas);
JSONObject jsonData = JSONObject.fromObject(datas);
JSONObject info = jsonData.getJSONObject("weatherinfo");
map.put("city", info.getString("city").toString());// 城市
map.put("temp", info.getString("temp").toString());// 温度
map.put("WD", info.getString("WD").toString());// 风向
map.put("WS", info.getString("WS").toString());// 风速
map.put("SD", info.getString("SD").toString());// 湿度
map.put("time", info.getString("time").toString());// 发布时间
} catch (SocketTimeoutException e) {
System.out.println("连接超时");
} catch (FileNotFoundException e) {
System.out.println("加载文件出错");
}
return map;
}
/**
*
* 获取实时天气2
* 方 法 名: getTodayWeather
*
* @param Cityid
* 城市编码
*/
public static Map getTodayWeather2(String Cityid)
throws IOException, NullPointerException {
// 连接中央气象台的API
URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + Cityid
+ ".html");
URLConnection connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
Map map = new HashMap();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
connectionData.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
String datas = sb.toString();
System.out.println(datas);
JSONObject jsonData = JSONObject.fromObject(datas);
JSONObject info = jsonData.getJSONObject("weatherinfo");
map.put("city", info.getString("city").toString());// 城市
map.put("temp1", info.getString("temp1").toString());// 最高温度
map.put("temp2", info.getString("temp2").toString());// 最低温度
map.put("weather", info.getString("weather").toString());//天气
map.put("ptime", info.getString("ptime").toString());// 发布时间
} catch (SocketTimeoutException e) {
System.out.println("连接超时");
} catch (FileNotFoundException e) {
System.out.println("加载文件出错");
}
return map;
}
private static String getWeek(int iw) {
String weekStr = "";
switch (iw) {
case 1:
weekStr = "星期天";
break;
case 2:
weekStr = "星期一";
break;
case 3:
weekStr = "星期二";
break;
case 4:
weekStr = "星期三";
break;
case 5:
weekStr = "星期四";
break;
case 6:
weekStr = "星期五";
break;
case 7:
weekStr = "星期六";
break;
default:
break;
}
return weekStr;
}
public static void main(String[] args) {
try {
//测试获取实时天气1(包含风况,湿度)
Map map = getTodayWeather1("101020100");
System.out.println(map.get("city") + "\t" + map.get("temp")
+ "\t" + map.get("WD") + "\t" + map.get("WS")
+ "\t" + map.get("SD") + "\t" + map.get("time"));
//测试获取实时天气2(包含天气,温度范围)
Map map2 = getTodayWeather2("101010100");
System.out.println(map2.get("city") + "\t" + map2.get("temp1")
+ "\t" + map2.get("temp2") + "\t" + map2.get("weather")
+ "\t" + map2.get("ptime"));
//测试获取一周天气
List> listData = getWeekWeatherMap("101010100");
for (int j = 0; j < listData.size(); j++) {
Map wMap = listData.get(j);
System.out.println(wMap.get("city") + "\t" + wMap.get("date_y")
+ "\t" + wMap.get("week") + "\t" + wMap.get("weather")
+ "\t" + wMap.get("temp") + "\t" + wMap.get("wind")
+ "\t" + wMap.get("fl"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java ip调天气预报接口_JAVA + WeatherWebService 实现天气预报接口调取相关推荐
- java 集合 接口_Java集合之Collection接口
1 - Java集合介绍 /* 1. 一方面, 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象 的操作,就要对对象进行存储. 2. 另一方面,使用Array存储对象方面具有一些弊 端,而 ...
- java 抽象接口_JAVA中的“抽象接口”
在程序设计过程中,读者很可能遇到这样一种困境:设计了一个接口,但实现这个接口的子类并不需要实现接口中的全部方法,也就是说,接口中的方法过多,对于某些子类是多余的,我们不得不浪费的写上一个空的实现. 今 ...
- javq接口_Java为什么要使用接口_java接口怎么使用
Java接口是什么 Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 接口 ...
- java ip调天气预报接口_java调天气预报接口
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- java ip归属地查询_JAVA版IP地址查询调用示例
package cn.juhe; import net.sf.json.JSONObject; import org.springframework.web.client.RestTemplate; ...
- java调用微信红包接口_java调用微信现金红包接口的心得与体会
这几天看了下以前写的有关微信支付的博客,看的人仍是挺多的,看了下留言不知道是由于博客写的不够细仍是什么状况,大多都找我要源码,我以为吧程序员仍是须要有这么一个思考的过程,所以没直接给源码,俗话说&qu ...
- java app微信支付接口_JAVA微信APP支付接口整合
上次我们看到了支付宝的APP支付工具,那么这次就来封装封装微信的APP支付;如果已经清楚了支付宝的支付流程,那么微信支付也和它大同小异了,当然这其中肯定是有各种变化的: 首先让我把微信支付文档的官网贴 ...
- java的标记接口_Java中的标记接口?
我被教授,Java中的Marker接口是一个空接口,用于向编译器或JVM发送信号,实现此接口的类的对象必须以特殊方式处理,如序列化,克隆等. 但最近我了解到,它实际上与编译器或JVM无关.例如,在Se ...
- java异步调用第三方接口_Java调用第三方系统接口获取数据
1,调取接口代码 RestTemplate restTemplate=new RestTemplate();HttpHeaders headers=new HttpHeaders();MultiVal ...
最新文章
- mysql 主从报错
- EasyTrack项目管理软件的四大版本和相关组件介绍
- mac搭建PHP开发环境
- uva 10285——Longest Run on a Snowboard
- 前端学习(803):字符串拼接和截取
- mysql 内置功能 函数 date_format函数
- 递归、os.walk、内置函数、lambda、hashlib模块、md5加密、python安装第三方模块、操作mysql数据库...
- 【Spring Boot】28.开发热部署
- [转]unresolved external symbol _*
- 安装oracle 10g 的艰难之旅
- 使用ffmpeg转换webm格式到MP4格式
- 昆仑通态复制的程序可以用吗_第478期丨相同功能带定时器的PLC程序怎么简化?非标设备出口到日本,应该怎么配电。...
- Web渗透测试实战(Metasploit5.0) 一
- 【060】助力一箭四星,翼辉系统再续辉煌
- 内核函数-assembler.h分析
- wordpress Avada主题banner制作
- oracle dul 扫描磁盘,案例:Oracle dul数据挖掘 磁盘损坏dul提取数据文件中表的数据及l...
- 超详细: Type-C接口Macbook笔记本无法充电(时连时断)的傻瓜处理流程
- 用Python做一个超简单的小游戏(一听就懂)
- 路由交换技术之代理ARP
热门文章
- python pexpect输出_关于多线程:如何使用pexpect获取python中子进程的自发输出
- html中把句子往右边弄一点咋办,CSS上的左,中,右对齐方法,可以在一行上混合...
- jdbc mysql api_JDBC Api详解
- for循环执行 mybatis_Mybatis中使用循环遍历
- android存到手机内存,android保存文件到手机内存
- procyon java8_java jdk 8反编译工具JD-GUI、procyon-decompiler、luyten、crf下载使用简介
- 【OpenCV 例程200篇】48. 图像增强—彩色直方图匹配
- 使用一个环境的或者半径异样消除器来进行异样消除
- springboot jpa sql打印_SpringBoot集成Spring Data JPA以及读写分离
- opencv图像分割2-GMM