Android HttpUrlConnection getInputStream 抛出 NullPointerException

Posted

技术标签:

【中文标题】Android HttpUrlConnection getInputStream 抛出 NullPointerException【英文标题】:Android HttpUrlConnection getInputStream throws NullPointerException 【发布时间】:2013-03-11 13:01:58 【问题描述】:

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

https://hme_player_pictures.s3.amazonaws.com/test-512813ed3b83286c72f376c7-thumb100.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。上面的网址在浏览器中确实有效。

【问题讨论】:

【参考方案1】:

不管怎样,我在 android HTTP 库中看到了很多错误。从条形码扫描仪中,我得到了大约 3500 万人的堆栈跟踪,所以我想我已经看到了一切。下面是我们在应用程序中捕获和吞下的所有奇怪的东西。我建议您将其作为一个平台错误解决并优雅地失败。

https://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/HttpHelper.java

  private static int safelyConnect(String uri, HttpURLConnection connection) throws IOException 
    try 
      connection.connect();
     catch (NullPointerException npe) 
      // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(npe.toString());
     catch (IllegalArgumentException iae) 
      // Also seen this in the wild, not sure what to make of it. Probably a bad URL
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(iae.toString());
     catch (SecurityException se) 
      // due to bad *** settings?
      Log.w(TAG, "Restricted URI? " + uri);
      throw new IOException(se.toString());
     catch (IndexOutOfBoundsException ioobe) 
      // Another Android problem? https://groups.google.com/forum/?fromgroups#!topic/google-admob-ads-sdk/U-WfmYa9or0
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(ioobe.toString());
    
    try 
      return connection.getResponseCode();
     catch (NullPointerException npe) 
      // this is maybe this Android bug: http://code.google.com/p/android/issues/detail?id=15554
      Log.w(TAG, "Bad URI? " + uri);
      throw new IOException(npe.toString());
     catch (NumberFormatException nfe) 
      // Again seen this in the wild for bad header fields in the server response!
      Log.w(TAG, "Bad server status? " + uri);
      throw new IOException(nfe.toString());
    
  

【讨论】:

类似的问题还有很多。我只是建议这不是您的应用程序做错了什么。在这种情况下,除了优雅地解决它之外没有什么可做的。 我的堆栈跟踪与 code.google.com/p/android/issues/detail?id=15554 不同,我认为这与 https 连接或亚马逊主机 url 有关。同样openConnection() 调用成功但getInputStream() 失败【参考方案2】:

怀疑问题出在 amazon s3 的 url 的主机名上。

所以我改变了

https://hme_player_pictures.s3.amazonaws.com/test-512813ed3b83286c72f376c7-thumb100.jpg

https://s3.amazonaws.com/hme_player_pictures/test-512813ed3b83286c72f376c7-thumb100.jpg

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

【讨论】:

问题可能是url中的“_”(下划线):developers.facebook.com/bugs/436848366360282

以上是关于Android HttpUrlConnection getInputStream 抛出 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Android 的 HttpUrlConnection 不支持 HTTP/2?

Android中的HttpURLConnection有线记录

[Android基础]Android中使用HttpURLConnection

Android HttpUrlConnection 无法正常工作

HttpUrlConnection.getInputStream 在 Android 中返回空流

android中的HttpUrlConnection的使用之二