3

我想從這個網址下載映像:的Android HttpURLConnection類的getInputStream引發NullPointerException異常

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。上面的網址在瀏覽器中工作。

回答

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()); 
    } 
    } 
+0

還有很多類似的問題。我只是暗示這不是你的應用程序做錯了。在這種情況下,除了優雅地解決它外,沒有什麼可做的。 – 2013-03-21 20:53:33

+0

我有不同的堆棧跟蹤比code.google.com/p/android/issues/detail?id=15554我認爲這是與https連接或amazons主機url有關。 ''openConnection()'調用成功,但'getInputStream()'失敗 – Saqib 2013-03-21 20:54:24

相關問題