天气预报接口

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class WeatherUtil {

private static String SERVICE_HOST = "www.webxml.com.cn";
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL + "getRegionProvince";
private static String CITY_CODE_URL = WEATHER_SERVICES_URL + "getSupportCityString?theRegionCode=";
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + "getWeather?theUserID=&theCityCode=";

/**
* @param args
*/
public static void main(String[] args) {
int provinceCode = getProvinceCode("辽宁");
System.out.println(provinceCode);
int cityCode = getCityCode(provinceCode, "大连");
System.out.println(cityCode);
List<String> weatherList = getWeather(cityCode);
for (String weather : weatherList) {
System.out.println(weather);
}
}

public static List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
documentBF.setNamespaceAware(true);
try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode);
document = documentB.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
inputStream.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return weatherList;
}

/**
* @param provinceCode
* @param cityName
* @return
*/
public static int getCityCode(int provinceCode, String cityName) {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int cityCode = 0;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();
for (int i = 0; i < len; i++) {
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if (cName.equalsIgnoreCase(cityName)) {
cityCode = Integer.parseInt(cCode);
}
}
is.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return cityCode;
}

public static int getProvinceCode(String provinceName) {
Document document;
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
documentBF.setNamespaceAware(true);
int provinceCode = 0;

try {
DocumentBuilder documentB = documentBF.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL);
document = documentB.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("string");
int len = nodeList.getLength();
for (int i = 0; i < len; i++) {
Node n = nodeList.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
System.out.println(pName + "---" + pCode);
if (pName.equalsIgnoreCase(provinceName)) {
provinceCode = Integer.parseInt(pCode);
}
}
inputStream.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return provinceCode;
}

public static InputStream getSoapInputStream(String url) {
InputStream inputStream = null;

try {
URL urlObj = new URL(url);
URLConnection urlConn = urlObj.openConnection();
urlConn.setRequestProperty("Host", SERVICE_HOST);
urlConn.connect();
inputStream = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return inputStream;
}
}

你可能感兴趣的:(天气预报)