问题获取XML文件中的Java [英] Problem fetching XML file in Java
本文介绍了问题获取XML文件中的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用谷歌的非官方天气API在Android应用程序。
I am trying to use Google's unofficial weather API in an Android Application.
我用这个code:
//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.printStackTrace();
}
SAXBuilder parser = new SAXBuilder();
try {
doc = parser.build(googleWeatherService);
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但我得到的错误java.io.IOException异常无法打开的http:// www.google.com/ig/api?weather=08003 的(只是用08003作为一个例子)。
But I get the error java.io.IOException Couldn't open http://www.google.com/ig/api?weather=08003 (just using 08003 as an example).
如果你去在FF的链接,你得到天气的一个很好的XML文件,所以我究竟做错了什么?
If you go to the link in FF you get a nice XML file of weather, so what am I doing wrong?
推荐答案
这工作完美的我:
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.printStackTrace();
}
finally
{
close(br);
}
}
}
private static void close(BufferedReader br)
{
try
{
if (br != null)
{
br.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
下面是它带回来的结果是:
Here's the result it brought back:
<?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>
这篇关于问题获取XML文件中的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文