OKHTTP:如何捕获 EBADF 异常

OKHTTP : how to catch the EBADF exception

在我的 Android 应用程序中使用 OkHTTP,我有很多例外情况: 例如:

java.lang.AssertionError: libcore.io.ErrnoException: getsockname failed: EBADF (Bad file number)
       at libcore.io.IoBridge.getSocketLocalPort(IoBridge.java:649)
       at libcore.io.IoBridge.closeSocket(IoBridge.java:202)
       at java.net.PlainSocketImpl.close(PlainSocketImpl.java:162)
       at ...
Caused by: libcore.io.ErrnoException: getsockname failed: EBADF (Bad file number)
       at libcore.io.Posix.getsockname(Posix.java)
       at libcore.io.ForwardingOs.getsockname(ForwardingOs.java:69)
       at libcore.io.IoBridge.getSocketLocalPort(IoBridge.java:645)
       at libcore.io.IoBridge.closeSocket(IoBridge.java:202)
       at java.net.PlainSocketImpl.close(PlainSocketImpl.java:162)
       at java.net.Socket.close(Socket.java:319)
       at ...

我无法捕捉到这个错误,因为它在 OkHTTP 库中。

有什么解决办法吗?

谢谢

我终于和 OkHttp 团队讨论了,现在修复在当前版本中!

OkHttp v2.5.0

OkIO v1.6.0

更多信息在这里: https://github.com/square/okhttp/pull/1817

根本原因是 android 4 错误: https://code.google.com/p/android/issues/detail?id=54072