我希望這將是一個簡單的。Android:我怎麼捕獲這個異常
我一個重新使用代碼,在一個列表視圖 分析並顯示RSS當從後面代理或沒有互聯網我獲得以下崩潰報告運行代碼的教程:
12-12 09:48:57.372: W/RssApp(1494): Exception while retrieving the input stream
12-12 09:48:57.372: W/RssApp(1494): java.net.ConnectException: failed to connect to gigglepics.co.uk/188.121.41.139 (port 80): connect failed: ETIMEDOUT (Connection timed out)
12-12 09:48:57.372: W/RssApp(1494): at libcore.io.IoBridge.connect(IoBridge.java:114)
12-12 09:48:57.372: W/RssApp(1494): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-12 09:48:57.372: W/RssApp(1494): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-12 09:48:57.372: W/RssApp(1494): at java.net.Socket.connect(Socket.java:842)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
12-12 09:48:57.372: W/RssApp(1494): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-12 09:48:57.372: W/RssApp(1494): at eu.MasterZangetsu.RssService.getInputStream(RssService.java:48)
12-12 09:48:57.372: W/RssApp(1494): at eu.MasterZangetsu.RssService.onHandleIntent(RssService.java:33)
12-12 09:48:57.372: W/RssApp(1494): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-12 09:48:57.372: W/RssApp(1494): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 09:48:57.372: W/RssApp(1494): at android.os.Looper.loop(Looper.java:137)
12-12 09:48:57.372: W/RssApp(1494): at android.os.HandlerThread.run(HandlerThread.java:60)
12-12 09:48:57.372: W/RssApp(1494): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
12-12 09:48:57.372: W/RssApp(1494): at libcore.io.Posix.connect(Native Method)
12-12 09:48:57.372: W/RssApp(1494): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
12-12 09:48:57.372: W/RssApp(1494): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-12 09:48:57.372: W/RssApp(1494): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-12 09:48:57.372: W/RssApp(1494): ... 20 more
12-12 09:48:57.382: W/dalvikvm(1494): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
12-12 09:48:57.402: E/AndroidRuntime(1494): FATAL EXCEPTION: IntentService[RssService]
12-12 09:48:57.402: E/AndroidRuntime(1494): java.lang.NullPointerException
12-12 09:48:57.402: E/AndroidRuntime(1494): at eu.MasterZangetsu.PcWorldRssParser.parse(PcWorldRssParser.java:33)
12-12 09:48:57.402: E/AndroidRuntime(1494): at eu.MasterZangetsu.RssService.onHandleIntent(RssService.java:33)
12-12 09:48:57.402: E/AndroidRuntime(1494): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-12 09:48:57.402: E/AndroidRuntime(1494): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 09:48:57.402: E/AndroidRuntime(1494): at android.os.Looper.loop(Looper.java:137)
12-12 09:48:57.402: E/AndroidRuntime(1494): at android.os.HandlerThread.run(HandlerThread.java:60)
這發生在inputStream.close();
public List<RssItem> parse(InputStream inputStream)
throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readFeed(parser);
} finally {
inputStream.close();
}
}
所有我想要做的就是抓住這個例外,以確保應用程序不會崩潰,而是顯示錯誤消息
一切,我嘗試似乎仍然崩潰,並給出了完全不同的錯誤。我必須失去了真正的基本的東西
在finally塊中添加try/catch塊沒有什麼可恥:)。檢查[這](http://stackoverflow.com/questions/6095664/putting-try-catch-finally-block-inside-another-finally-block) –
正如下面的Kevin Bowersox評論所述,你的問題不在inputStream 。關(); –
我補充說他建議不起作用。其他建議? –