该博文描述的是一个调用气象局天气预报的接口,可作为工具类直接使用,有什么不妥的,请各路大神指教!
package com.gobestsoft.core.util;
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 RoninGirl
*
*/
public class WeatherUtil {
/**
* 获取一周天气<br>
* 方 法 名:getWeekWeatherMap <br>
* @param Cityid 城市编码
*/
public static List<Map<String, Object>> 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<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
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()