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 实现天气预报接口调取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机应用头部案例提交,基于头部位置的应
- 下一篇: matlab欧拉法程序,欧拉法matla