??242 2016-11-09 01:34 采纳率: 0%
浏览 1060

Java新手 URLConnection 调用出错

新手看一个调用API获取天气的例程,实际运行时出现错误,得不到请求的结果,求各位看看是哪的问题

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);
        System.out.println("getInputStream Executed");
        URLConnection conn = U.openConnection();
        conn.setRequestProperty("Host", SERVICES_HOST);
        conn.connect();
        is = conn.getInputStream();
        System.out.println(is);
    } 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("市");    //错误:此处数字超出范围,说明没有获取到province 值,回溯上面的doc发现值为documen:NUll

                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;

}

错误截图:
图片说明

我看有连接服务器是本地的,是不是连接网络服务器需要设置什么?
还是服务器API接口已经不能用了?
图中输出是我加的标记,sun.net那一行是getSoapInputStream()方法里最终获得的InputStream输出。
谢谢各位大神了

  • 写回答

2条回答 默认 最新

  • 祈祷爱绝缘 2016-11-09 01:40
    关注

    看报错是你getRegion方法里面数组越界了。

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序