在Java中获取XML文件时出现问题

在Java中获取XML文件时出现问题,第1张

概述我正在尝试在Android应用程序中使用Google的非官方天气API.我用这个代码://getthetextfromtheedittextuserZip=zipCode.getText().toString();//createalinkusingthezipcode//TODOsanitizeinputSystem.out.println(userZip);link="ht

我正在尝试在Android应用程序中使用Google的非官方天气API.

我用这个代码:

//get the text from the edit text    userZip = zipCode.getText().toString();    //create a link using the zip code    //Todo sanitize input    System.out.println(userZip);    link = "http://www.Google.com/ig/API?weather=" + userZip;    System.out.println(link);    //connect to the link    URL GoogleWeatherService = null;    try {        GoogleWeatherService = new URL(link);    } catch (MalformedURLException e1) {        // Todo auto-generated catch block        e1.@R_502_1715@();    }     SAXBuilder parser = new SAXBuilder();    try {        doc = parser.build(GoogleWeatherService);    } catch (JDOMException e) {        // Todo auto-generated catch block        e.@R_502_1715@();    } catch (IOException e) {        // Todo auto-generated catch block        e.@R_502_1715@();    }

但我收到错误java.io.IOException无法打开http://www.google.com/ig/api?weather=08003(仅以08003为例).

如果你去FF中的链接,你会得到一个很好的天气XML文件,那么我做错了什么?

解决方法:

这对我很有用:

package weather;import org.dom4j.document;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import java.io.BufferedReader;import java.io.inputStreamReader;import java.net.URL;/** * GoogleWeather * @author Michael * @since 2/12/11 */public class GoogleWeather{    public static voID main(String[] args)    {        for (String userZip : args)        {            BufferedReader br = null;            try            {                String link = "http://www.Google.com/ig/API?weather=" + userZip;                System.out.println(link);                URL GoogleWeatherService = new URL(link);                br = new BufferedReader(new inputStreamReader(GoogleWeatherService.openStream()));                SAXReader reader = new SAXReader();                document document = reader.read(GoogleWeatherService);                OutputFormat format = OutputFormat.createPrettyPrint();                XMLWriter writer = new XMLWriter(System.out, format);                writer.write(document);            }            catch (Exception e)            {                e.@R_502_1715@();            }            finally            {                close(br);            }        }    }    private static voID close(BufferedReader br)    {        try        {            if (br != null)            {                br.close();            }        }        catch (Exception e)        {            e.@R_502_1715@();        }    }}

这是它带来的结果:

<?xml version="1.0" enCoding="UTF-8"?><xml_API_reply version="1">  <weather module_ID="0" tab_ID="0" mobile_row="0" mobile_zipped="1" row="0" section="0">    <forecast_information>      <city data="Hebron, CT"/>      <postal_code data="06248"/>      <latitude_e6 data=""/>      <longitude_e6 data=""/>      <forecast_date data="2011-02-12"/>      <current_date_time data="2011-02-13 03:00:47 +0000"/>      <unit_system data="US"/>    </forecast_information>    <current_conditions>      <condition data="Partly Cloudy"/>      <temp_f data="28"/>      <temp_c data="-2"/>      <humIDity data="HumIDity: 45%"/>      <icon data="/ig/images/weather/partly_cloudy.gif"/>      <wind_condition data="Wind: NW at 14 mph"/>    </current_conditions>    <forecast_conditions>      <day_of_week data="Sat"/>      <low data="16"/>      <high data="36"/>      <icon data="/ig/images/weather/partly_cloudy.gif"/>      <condition data="Partly Cloudy"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Sun"/>      <low data="30"/>      <high data="38"/>      <icon data="/ig/images/weather/sNow.gif"/>      <condition data="SNow Showers"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Mon"/>      <low data="23"/>      <high data="46"/>      <icon data="/ig/images/weather/cloudy.gif"/>      <condition data="Cloudy"/>    </forecast_conditions>    <forecast_conditions>      <day_of_week data="Tue"/>      <low data="12"/>      <high data="29"/>      <icon data="/ig/images/weather/cloudy.gif"/>      <condition data="Windy"/>    </forecast_conditions>  </weather></xml_API_reply>
总结

以上是内存溢出为你收集整理的在Java中获取XML文件时出现问题全部内容,希望文章能够帮你解决在Java中获取XML文件时出现问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1096925.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存