java.sql.SQLException:网络错误IOException:连接失败

时间:2015-01-06 06:45:11

标签: android socketexception

它在AVD工作,但同样的事情,当我尝试在Android设备中,它给出以下错误

java.sql.SQLException: Network error IOException: failed to connect to /192.168.0.226 (port 1433): connect failed: ETIMEDOUT (Connection timed out)
 java.sql.SQLException: Network error IOException: failed to connect to /192.168.0.226 (port 1433): connect failed: ETIMEDOUT (Connection timed out)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:372)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:181)
    at java.sql.DriverManager.getConnection(DriverManager.java:175)
    at java.sql.DriverManager.getConnection(DriverManager.java:209)
    at com.example.database1.MainActivity.query2(MainActivity.java:71)
    at com.example.database1.MainActivity$1.onClick(MainActivity.java:51)
    at android.view.View.performClick(View.java:4101)
    at android.view.View$PerformClick.run(View.java:17082)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4954)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.net.ConnectException: failed to connect to /192.168.0.226 (port 1433): connect failed: ETIMEDOUT (Connection timed out)
    at libcore.io.IoBridge.connect(IoBridge.java:114)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.Socket.startupSocket(Socket.java:565)
    at java.net.Socket.tryAllAddresses(Socket.java:127)
    at java.net.Socket.<init>(Socket.java:177)
    at java.net.Socket.<init>(Socket.java:149)
    at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:252)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:297)
    ... 16 more
 Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
    at libcore.io.Posix.connect(Native Method)
    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    at libcore.io.IoBridge.connect(IoBridge.java:112)
    ... 24 more

1 个答案:

答案 0 :(得分:0)

也许您的真实设备无法访问为数据库提供服务的主机。

确保您的Android设备与数据库服务器位于同一子网(可能是相同的WiFi)。

还要确保数据库不仅侦听localhost(127.0.0.1),而是侦听所有网络接口(0.0.0.0)。如果Android模拟器在与数据库服务器相同的计算机上运行,​​则只需检查此项。

最后一次确保没有防火墙阻止请求。

祝你好运!