Android HttpUrlConnection的getInputStream方法抛出NullPointerException异常。

3
我正在尝试从此URL下载图片: 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。上述url在浏览器中可以工作。

2个回答

3
就我所知,我看到了许多与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 VPN 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());
    }
  }

有很多类似的问题。我只是建议这不是你的应用程序出了问题。在这种情况下,除了优雅地解决它,没有太多事情可做。 - Sean Owen
我遇到的问题与code.google.com/p/android/issues/detail?id=15554不同,我认为这可能与https连接或亚马逊主机URL有关。此外,openConnection()调用成功,但getInputStream()失败。 - Saqib

1

1
问题可能出在url中的"_"(下划线):http://developers.facebook.com/bugs/436848366360282/ - José Castro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接