代码出错 帮忙看一下

wangxl3 2009-09-02 11:59:09
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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.DOMException;
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 SERVICES_HOST = "www.webxml.com.cn";
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
private static String GET_CITY_CODE = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";

private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";

private static String GET_REGION_BY_IP = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext";

private static String GET_PROVINCE_CODE = WEATHER_SERVICES_URL
+ "getRegionProvince";

private WeatherUtil(){}

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

try {
URL U = new URL(url);
URLConnection conn = U.openConnection();
conn.setRequestProperty("Host", SERVICES_HOST);
conn.connect();
is = conn.getInputStream();
} catch (MalformedURLException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
return is;
}


public static List<String> getWeather(int cityCode)
{
List<String> weatherList = new ArrayList<String>();
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);

try {
DocumentBuilder db = dbf.newDocumentBuilder();

InputStream is = getSoapInputStream(WEATHER_QUERY_URL
+ cityCode);
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 weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
is.close();
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
} catch (DOMException e) {

e.printStackTrace();
} catch (ParserConfigurationException e) {

e.printStackTrace();
} catch (SAXException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
return weatherList;
}

public static void main(String[] args) throws Exception
{

getRegion();

String[] pInfo = getRegion();

String provinceName = pInfo[0];
String cityName = pInfo[1];

int provinceCode = getProvinceCode(provinceName);

int cityCode = getCityCode(provinceCode, cityName);

List<String> weatherList = WeatherUtil.getWeather(cityCode);

for (String weather : weatherList)
{
System.out.println(weather);
}

}

public static String[] getRegion()
{
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
String[] regionInfo = new String[5];

try {

DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_REGION_BY_IP);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
int len = nl.getLength();

for (int i = 0; i < len; i++)
{
if(i == 1)
{
Node n = nl.item(i);

String[] provinceInfo = n.getFirstChild().getNodeValue().split("省");

String province = provinceInfo[0];

String[] cityInfo = provinceInfo[1].split("市");

String city = cityInfo[0];

regionInfo[0] = province;
regionInfo[1] = city;

}

}
is.close();

} catch (DOMException e) {

e.printStackTrace();
} catch (ParserConfigurationException e) {

e.printStackTrace();
} catch (SAXException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

return regionInfo;

}

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

try {

DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(GET_PROVINCE_CODE);
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 pName = address[0];
String pCode = address[1];

if(pName.equalsIgnoreCase(provinceName))
{
provinceCode = Integer.parseInt(pCode);
}

}
is.close();

} catch (DOMException e) {

e.printStackTrace();
} catch (ParserConfigurationException e) {

e.printStackTrace();
} catch (SAXException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

return provinceCode;

}

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(GET_CITY_CODE + 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 (DOMException e) {

e.printStackTrace();
} catch (ParserConfigurationException e) {

e.printStackTrace();
} catch (SAXException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

return cityCode;

}


}


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at WeatherUtil.getRegion(WeatherUtil.java:150)
at WeatherUtil.main(WeatherUtil.java:105)


...全文
69 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqhweb 2009-09-03
  • 打赏
  • 举报
回复
java.lang.ArrayIndexOutOfBoundsException: 1

下标越界
imasmallbird 2009-09-03
  • 打赏
  • 举报
回复
数组下标越界了
provinceInfo这个里面只有一个数组元素:provinceInfo[0]=“中国 移动”
所以当你试图使用provinceInfo[1]时就会出异常

62,621

社区成员

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

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