gpt4 book ai didi

java - Android:如何捕获此异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:44 25 4
gpt4 key购买 nike

我希望这将是一个简单的。

我重用了教程中的代码,该代码在 ListView 中解析和显示 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();

}
}

我想做的就是捕捉这个异常,以确保应用程序不会崩溃,而是显示一条错误消息

我尝试的一切似乎仍然崩溃并给出一个完全不同的错误。我一定是遗漏了一些非常基本的东西

最佳答案

代码缺少实际的 catch 语句:

try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readFeed(parser);
}catch(IllegalArgumentException e){ //Replace this with the more specific exception
//do something
}catch(Excpetion e){ //You can have multiple catch blocks from specific to general
//do something
}finally {
inputStream.close();

}

添加此代码后,您仍然会收到错误,因为 inputStream 为 null 并在 finally block 中抛出 NullPointerException。有两种方法可以处理这个问题。

第一种是简单地在 finally block 中捕获 NullPointerException:

try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(inputStream, null);
parser.nextTag();
return readFeed(parser);
}catch(IllegalArgumentException e){ //Replace this with the more specific exception
//do something
}catch(Excpetion e){ //You can have multiple catch blocks from specific to general
//do something
}finally {
try{
inputStream.close();
}catch(Exception e){
//do something
}
}

第二种是在方法的开头添加一个保护条件,不接受空的InputStream

public List<RssItem> parse(InputStream inputStream)
throws IllegalArgumentException {

if(inputStream == null){
throw new IllegalArgumentException();
}
}

可以这样称呼:

  try{
someObject.parse(null);
}catch(IllegalArgumentException e){
//do something
}

关于java - Android:如何捕获此异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20539728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com