url.openstream()或urlconnection.getinputstream()提高NullPointerException异常上有效的URL [英] url.openstream() or urlconnection.getinputstream() raise nullpointerexception on valid URL

查看:589
本文介绍了url.openstream()或urlconnection.getinputstream()提高NullPointerException异常上有效的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关Android应用程序,我想改变,因为新服务器的XML的URL,但内容是一样的。 我曾经这样做:

For an android application I am trying to change an URL of an XML because of a new server, but the content is the same. I used to do this:

InputSource ins = new InputSource(rssUrl.openStream());

这曾经工作和几天仍然没有,而旧的URL可在旧服务器上。但是,当我改变XML的URL我得到一个NullPointerException异常上面的行。

This used to work and for a few days still does, while the old URL is available on on the old server. But when i change the URL of the XML I get an NullPointerException on the row above.

我肯定rssUrl不为空,我打印了它的价值仅仅是工作的网址。我验证了XML和XML是有效的,但是这并不重要,因为这个问题是在分析开始前提出。

I am sure rssUrl is not null, I printed the value of it an is just the working URL. I validated the XMl and the XML is valid, but this doesn't really matter because the problem is raised before the parsing starts.

的事情之一,我想是这样的:

One of the things I tried was something like this:

Urlconnection urlConnection= rssUrl.openurlconnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
inputsource ins = new inputsource(in);

但是这提出了一个NullPointerException异常也是如此。

but this raised an nullpointerexception as well.

我读了一些东西有关降级我的Java 6 (目前我使用7)。但我真的不喜欢它,不能想象为什么它会工作。

I read some things about downgrading my Java to 6 (currently i am using 7). But i don't really feel like it and can't imagine why it would work.

编辑: 我试图降级到Java SE 6中,但没有奏效。

edit: I tried downgrading to java se 6 but didn't work.

有谁知道这个错误是可能的,如何解决呢?

Does anyone know how this error is possible and how to resolve it?

堆栈:

12-28 22:14:53.635: W/System.err(6949): java.lang.NullPointerException
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343)
12-28 22:14:53.725: W/System.err(6949):     at java.util.HashMap.get(HashMap.java:298)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:67)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12-28 22:14:53.725: W/System.err(6949):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
12-28 22:14:53.735: W/System.err(6949):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
12-28 22:14:53.735: W/System.err(6949):     at java.net.URL.openStream(URL.java:462)
12-28 22:14:53.735: W/System.err(6949):     at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:610)
12-28 22:14:53.735: W/System.err(6949):     at com.hera.ontdekdelft.StartUp$LoadData.doInBackground(StartUp.java:1)
12-28 22:14:53.735: W/System.err(6949):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-28 22:14:53.735: W/System.err(6949):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-28 22:14:53.735: W/System.err(6949):     at j.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-28 22:14:53.735: W/System.err(6949):     at java.lang.Thread.run(Thread.java:856)
ava.util

解决方案

下划线的URL导致Android的失败。我的解决方案是编辑XML文件,以便rssUrl.openstream()的URL会再次合作。问题解决了,感谢 Kiruwka

Underscores in the URL caused android to fail. My solution was editing the url of the XML file so rssUrl.openstream() would work again. problem solved, thanks to Kiruwka

推荐答案

根据您所提供的似乎 uri.getHost()为空,抛出跟踪 NPE 来从内部实现。你可以验证它是否是空通过输出:

According to the trace you provided it seems that uri.getHost() is null, throwing NPE to you from internal implementation. You could verify if it is null by outputting :

Log.d(TAG, "host = " + rssUrl.getHost());

打开连接之前。如果为null,解决您的网址。

before opening connection. If it is null, fix your url.

更新
我看到了几个类似的问题的情况下,当主字符串包含空格或_字符,例如的这个已知的错误该解决方案以下划线符号的问题。

Update
I saw several similar issues for cases when host string contains spaces or '_' characters, for example this known bug, or This solution for underscore symbols issue.

这篇关于url.openstream()或urlconnection.getinputstream()提高NullPointerException异常上有效的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆