Java实现天气预报查询接口调用功能,想实现,但是省份,城市的显示不出来,这是为什么,哪地方出错了,求解答,非常感谢!

jihejiage 2017-02-27 10:23:02
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
//http://www.webxml.com.cn/WebServices/WeatherWS.asmx

/**
* WeatherUtil 天气预报接口调用的核心类
* @author King
*
*/
public class WeatherUtil {
//主机地址(可以根据主机地址获取第三方地址)Host:www.webxml.com.cn请求头
private static String SERVICES_HOST = "ws.webxml.com.cn";
//天气预报Web服务主机
private static String WEATHER_SERVICES_URL = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/";
//获取中国省份、直辖市、地区和与之对应的ID
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_CODE_URL = WEATHER_SERVICES_URL+"getWeather?theUserID=9ac66a483ee0490f9fe81a4c04603415&theCityCode=";


/**
* 访问第三方接口,打开服务器主机方法
* 方法名:getSoapInputStream
* @param thirdUrl
* @return
*/
//thirdUrl需要访问第三方接口
public static InputStream getSoapInputStream(String thirdUrl) {
InputStream inputStream=null;
try {
//通过http协议访问第三方接口
URL url=new URL(thirdUrl);
//打开一个链接,返回链接对象
URLConnection urlConnection=url.openConnection();
//设置主机名字,放在http header里(post)。setRequestProperty()告诉服务器,客户端的配置/需求
urlConnection.setRequestProperty("HOST", SERVICES_HOST);
//发出请求,与第三地址连接
urlConnection.connect();
//跟第三方接口连接后,返回一个流
inputStream=urlConnection.getInputStream();

} catch(Exception e) {
e.printStackTrace();
}
return inputStream;
}

/**
* getProviceCode() 获取省份编码
* @param provinceName 省份的名称
* @return
*/
public static int getProvinceCode(String provinceName) {
InputStream inputStream = null;
Document document = null;
int provinceId=0; //省份id
try {
//对读取的数据(xml格式)流进行解析,1)在工厂里获得解析器 2)通过解析器获得文档对象 3)通过解析器进行解析
//得到dom解析器的工厂实例documentBF
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
//得到解析器documentB
DocumentBuilder documentB = documentBF.newDocumentBuilder();
//读取xml格式数据流inputStream
inputStream = getSoapInputStream(PROVINCE_CODE_URL);
//解析xml文档的输入流(主机连接第三方接口得到的),得到一个document文档对象
document = documentB.parse(inputStream);

//获取文档的所有节点,DOM结构,元素节点,属性节点,文本节点
//获取包含String的节点集合
/**
* <?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns="http://WebXml.com.cn/">
<string>string</string>
<string>string</string>
</ArrayOfString>
*/
NodeList nodeList = document.getElementsByTagName("String");
int len = nodeList.getLength();
for(int i=0;i<len;i++) {
Node n = nodeList.item(i); //拿到每一个节点
//该文档<string>下只有第一个节点的string是需要的,所以是getFirstChild()
String value = n.getFirstChild().getNodeValue();
//<string>湖南,1111</string> 第一个值代表省份,第二个值代表省份的ID
//通过split()方法将省份和省份ID分开,并放入数组
String[] address = value.split(",");
String pName = address[0];
String pCode = address[1];
if(provinceName.equals(pName)) {
provinceId = Integer.parseInt(pCode);
}
}
} catch(Exception e) {

} finally {
try {
if(null!=inputStream) inputStream.close();
} catch(IOException e) {
e.printStackTrace();
}

}
return provinceId;
}

/**
* 获取城市编码
* @param provinceCode 省份代码
* @param cityName 城市名称
* @return
*/
public static int getCityCode(int provinceCode,String cityName) {
InputStream inputStream = null;
Document document = null;
int cityCode = 0;
try {
//得到Dom解析器的工厂实例
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBF.newDocumentBuilder();
inputStream = getSoapInputStream(CITY_CODE_URL+provinceCode);
document = builder.parse(inputStream);
NodeList nodeList = document.getElementsByTagName("String");
int len = nodeList.getLength();
for (int i = 0;i < len;i++) {
Node n = nodeList.item(i);
String value = n.getFirstChild().getNodeValue();
String[] adress = value.split(",");
String cName = adress[0];
String cCode = adress[1];
if(cName.equals(cityName)) {
cityCode = Integer.parseInt(cCode);
}
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null!=inputStream) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return cityCode;
}

/**
* 获取天气信息
* 方法名:getWeather()
* @param cityCode 城市编码
* @return List<String> 天气信息(信息比较多,所以放在List集合中)
*/
public static List<String> getWeather(int cityCode) {
List<String> weatherList = new ArrayList<String> ();
InputStream inputStream = null;
Document document = null;
try {
DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBF.newDocumentBuilder();
inputStream = getSoapInputStream(WEATHER_CODE_URL+cityCode);
document = builder.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();
weatherList.add(result);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weatherList;
}

public static void main(String[] args) {
int provinceCode = getProvinceCode("江苏");
System.out.println("省份: "+provinceCode);
int cityCode = getCityCode(provinceCode,"徐州");
System.out.println("城市: "+cityCode);
List<String> list = getWeather(cityCode);
for(String s:list) {
System.out.println(s);
}
}
}
请问代码哪地方出了问题?新手,可以的话,请详细一点解答,非常感谢~
...全文
321 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jihejiage 2017-02-27
  • 打赏
  • 举报
回复
程序运行没有错误。就是输出的结果是provinceCode=0,cityCode=0,可能是调用天气预报服务器那出错了,不知道怎么改,求指明一条方法~
  • 打赏
  • 举报
回复
是报错了还是怎么滴了
Defonds 2017-02-27
  • 打赏
  • 举报
回复
肉眼调 bug? 你在关键的步骤中 debug 一下,或者打印一下,把错误范围锁定一下

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧