Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法捕获从套接字引发的SocketTimeoutException。连接(InetAddress,超时)_Java_Android_Sockets - Fatal编程技术网

Java 无法捕获从套接字引发的SocketTimeoutException。连接(InetAddress,超时)

Java 无法捕获从套接字引发的SocketTimeoutException。连接(InetAddress,超时),java,android,sockets,Java,Android,Sockets,我正在开发一个应用程序,它通过套接字连接到服务器以读取和写入数据。 我在AsyncTask中使用runInBackground()方法来完成此任务 我将我的电脑用作服务器,并将我的android设备连接到WiFi以连接到电脑 我想模拟服务器不可用的情况,因此我断开设备与WiFi的连接并运行代码 我使用以下代码: try { socket = new Socket(); socket.connect(new InetSocketAddress("192.168.1.104", 4

我正在开发一个应用程序,它通过套接字连接到服务器以读取和写入数据。 我在AsyncTask中使用runInBackground()方法来完成此任务

我将我的电脑用作服务器,并将我的android设备连接到WiFi以连接到电脑

我想模拟服务器不可用的情况,因此我断开设备与WiFi的连接并运行代码

我使用以下代码:

try 
{
    socket = new Socket();
    socket.connect(new InetSocketAddress("192.168.1.104", 4444), 10000);
    ...
    ...
}
catch (SocketTimeoutException e) 
{
    e.printStackTrace();
}
catch (ClassNotFoundException e) 
{
    e.printStackTrace();
}
catch (IOException e) 
{
    e.printStackTrace();
}
catch (Exception e) 
{
    e.printStackTrace();
}
SocketTimeoutException在10秒后按预期抛出,但问题是没有一个catch块实际捕获该异常。这怎么可能

这是日志:

11-09 17:57:15.286: W/System.err(12050): java.net.SocketTimeoutException: failed to connect to     /192.168.1.104 (port 4444) after 10000ms
11-09 17:57:15.301: W/System.err(12050):    at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
11-09 17:57:15.301: W/System.err(12050):    at libcore.io.IoBridge.connect(IoBridge.java:112)
11-09 17:57:15.301: W/System.err(12050):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-09 17:57:15.301: W/System.err(12050):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-09 17:57:15.306: W/System.err(12050):    at java.net.Socket.connect(Socket.java:842)
11-09 17:57:15.306: W/System.err(12050):    at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:183)
11-09 17:57:15.306: W/System.err(12050):    at com.elyakimsportal.communication.ContactServer$RetrieveProfilesTask.doInBackground(ContactServer.java:1)
11-09 17:57:15.311: W/System.err(12050):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-09 17:57:15.311: W/System.err(12050):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-09 17:57:15.311: W/System.err(12050):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-09 17:57:15.311: W/System.err(12050):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-09 17:57:15.316: W/System.err(12050):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-09 17:57:15.316: W/System.err(12050):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-09 17:57:15.316: W/System.err(12050):    at java.lang.Thread.run(Thread.java:856)
11-09 17:57:15.316: I/System.out(12050): closed connection to server
另外,我还查看了Socket.connect(InetSocketAddress,timeout)方法的文档,它没有说它曾经抛出过SocketTimeoutException,这使得它更加混乱

Parameters
remoteAddr  the address and port of the remote host to connect to. 
timeout  the timeout value in milliseconds or 0 for an infinite timeout. 

Throws
IllegalArgumentException  if the given SocketAddress is invalid or not supported or the timeout value is negative. 
IOException  if the socket is already connected or an error occurs while connecting.  

顺便说一下,我的应用程序没有崩溃。我是否正确地假设这是因为它位于不同的线程中?

在我看来,在您提供的示例代码中,
SocketTimeoutException
被捕获。这是有道理的,因为你说过你的应用不会崩溃

由于您正在使用
e.printStackTrace()
将堆栈轨迹打印到终端,因此它可能仍然“看起来”像是崩溃或没有捕获异常

如果您不想用这些异常填写日志,请考虑重写:

try {
    socket = new Socket();
    socket.connect(new InetSocketAddress("192.168.1.104", 4444), 10000);
    ...
    ...

} catch (SocketTimeoutException e) {
    System.err.println("SocketTimeoutException: " + e.getMessage());

} catch (ClassNotFoundException e) {
    System.err.println("ClassNotFoundException: " + e.getMessage());

} catch (IOException e) {
    System.err.println("IOException: " + e.getMessage());

} catch (Exception e) {
    System.err.println("Exception: " + e.getMessage());

}

我面对这漫长的退路,成功地解决了它。 无法捕获异常的原因是,正在从调用线程的其他线程引发异常


如果您调试它,您可以看到线程。

您正在捕获它,然后打印堆栈跟踪…是的,我没有意识到,谢谢。顺便问一下,您能否解释一下,当文档没有提到引发此类异常的可能性时,为什么它会引发SocketTimeoutException?并非所有异常都需要专门引发。例如RuntimeException或者,更好的是,SocketTimeoutException属于IOException,我非常确定您的类至少抛出了这个(包括STE)OK now I feel Dumble:/感谢您为我清除它James:)