Android HttpUrlConnection getInputStream 抛出 NullPointerException [英] Android HttpUrlConnection getInputStream throws NullPointerException

查看:41
本文介绍了Android HttpUrlConnection getInputStream 抛出 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此网址下载图片:

I am trying to download the image from this url:

https://hme_player_pictures.s3.amazonaws.com/test-563278c78c72f376c7-thumb100.jpg拇指100.jpg

这里是堆栈跟踪:

03-21 12:58:04.040: W/System.err(7084): java.lang.NullPointerException
03-21 12:58:04.040: W/System.err(7084):     at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343)
03-21 12:58:04.045: W/System.err(7084):     at java.util.HashMap.get(HashMap.java:298)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:67)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-21 12:58:04.050: W/System.err(7084):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
03-21 12:58:04.055: W/System.err(7084):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-21 12:58:04.060: W/System.err(7084):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-21 12:58:04.065: W/System.err(7084):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-21 12:58:04.065: W/System.err(7084):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-21 12:58:04.070: W/System.err(7084):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
03-21 12:58:04.070: W/System.err(7084): ...

代码:

URL imageUrl = new URL(url);
HttpURLConnection c = (HttpURLConnection)imageUrl.openConnection();
InputStream in = c.getInputStream(); // Nullpointer exception on this line, c is definitely not null, I debugged

无法弄清楚为什么它会抛出 NullPointerException.上面的 url 在浏览器中确实有效.

Can't figure out why it is throwing NullPointerException. The above url does work in the browser.

推荐答案

因为怀疑问题是 amazon s3 的 url 主机名.

As suspected issue was with host name of url of amazon s3.

所以我改变了

https://hme_player_pictures.s3.amazonaws.com/test-563278c78c72f376c7-thumb100.jpg拇指100.jpg

https://s3.amazonaws.com/hme_player_pictures/test-563278c78c72f376c7-thumb100.jpg拇指100.jpg

它奏效了.我的 Java 代码现在很开心!

and it worked. My Java code is happy now!

这篇关于Android HttpUrlConnection getInputStream 抛出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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