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

我想在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.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为例)。

如果你去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.printStackTrace(); } finally { close(br); } } } private static void close(BufferedReader br) { try { if (br != null) { br.close(); } } catch (Exception e) { e.printStackTrace(); } } } 

这是它带来的结果:

                                                   

我认为您需要打开与url的连接并获取输入流以使其正常工作。 我会试试这个:

  URL googleWeatherService = null; URLConnection conn = null; try { googleWeatherService = new URL(link); conn = googleWeatherService.openConnection(); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } SAXBuilder parser = new SAXBuilder(); try { doc = parser.build(conn.getInputStream()); 

希望这对您有所帮助!

否则如果失败,听起来你必须处理URL重定向,这是我以前遇到的问题。 在这种情况下,您需要执行以下操作:

  URL googleWeatherService = null; URLConnection conn = null; try { googleWeatherService = new URL(link); HttpURLConnection ucon = (HttpURLConnection) googleWeatherService.openConnection(); ucon.setInstanceFollowRedirects(false); URL secondURL = new URL(ucon.getHeaderField("Location")); conn = secondURL.openConnection(); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } SAXBuilder parser = new SAXBuilder(); try { doc = parser.build(conn.getInputStream()); 

希望这能解决它!

您是否能够成功检索其他URI?

您可能遇到JVM配置问题。 在我遇到的大多数环境中,如果您的计算机已配置为Web浏览器可以成功发出HTTP请求,那么Java也可以成功实现。 但是我听说当你在代理服务器后面时需要特殊的JVM配置,我不知道在Android环境中是否需要类似的东西。

我用JDOM2遇到了这个问题,它实际上是伪装的NetworkOnMainThreadException。 抛弃主线程,一切正常。

 new Thread(new Runnable() { @Override public void run() {  } }).start();