2

我在 Android 上遇到了 PostgreSQL JDBC 驱动程序的问题。
我使用postgresql-9.2-1002.jdbc4.jar JDBC 驱动程序。

这是我的代码:

package xyz;

import java.sql.*;

public class BaseManager {

private String baseUrl = "jdbc:postgresql://0.0.0.0/database?user=username&password=secret";

private Connection conn = null;

public Integer getUserId(String login)
{   
    Integer result = -1;
    try {
        Class.forName("org.postgresql.Driver");

        conn = DriverManager.getConnection(baseUrl);
        //query here
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }
        return result;      
}
}

当我运行这个方法时,我得到了 NullPointerException:

04-29 15:24:43.458: W/System.err(549): org.postgresql.util.PSQLException: Something unusual has occured to cause the driver to fail. Please report this exception.
04-29 15:24:43.468: W/System.err(549):  at org.postgresql.Driver.connect(Driver.java:287)
04-29 15:24:43.468: W/System.err(549):  at java.sql.DriverManager.getConnection(DriverManager.java:175)
04-29 15:24:43.468: W/System.err(549):  at java.sql.DriverManager.getConnection(DriverManager.java:140)
04-29 15:24:43.468: W/System.err(549):  at xyz.BaseManager.getUserId(BaseManager.java:20)
04-29 15:24:43.468: W/System.err(549):  at xyz.LoginActivity.checkPassword(LoginActivity.java:50)
04-29 15:24:43.478: W/System.err(549):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:24:43.478: W/System.err(549):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 15:24:43.478: W/System.err(549):  at android.view.View$1.onClick(View.java:3039)
04-29 15:24:43.478: W/System.err(549):  at android.view.View.performClick(View.java:3511)
04-29 15:24:43.478: W/System.err(549):  at android.view.View$PerformClick.run(View.java:14105)
04-29 15:24:43.488: W/System.err(549):  at android.os.Handler.handleCallback(Handler.java:605)
04-29 15:24:43.488: W/System.err(549):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 15:24:43.488: W/System.err(549):  at android.os.Looper.loop(Looper.java:137)
04-29 15:24:43.498: W/System.err(549):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-29 15:24:43.498: W/System.err(549):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:24:43.498: W/System.err(549):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 15:24:43.498: W/System.err(549):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-29 15:24:43.518: W/System.err(549):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-29 15:24:43.518: W/System.err(549):  at dalvik.system.NativeStart.main(Native Method)
04-29 15:24:43.518: W/System.err(549): Caused by: android.os.NetworkOnMainThreadException
04-29 15:24:43.528: W/System.err(549):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-29 15:24:43.528: W/System.err(549):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-29 15:24:43.528: W/System.err(549):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-29 15:24:43.538: W/System.err(549):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-29 15:24:43.538: W/System.err(549):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-29 15:24:43.548: W/System.err(549):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
04-29 15:24:43.558: W/System.err(549):  at java.net.Socket.connect(Socket.java:842)
04-29 15:24:43.558: W/System.err(549):  at java.net.Socket.connect(Socket.java:785)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.core.PGStream.<init>(PGStream.java:60)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
04-29 15:24:43.568: W/System.err(549):  at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
04-29 15:24:43.578: W/System.err(549):  at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
04-29 15:24:43.578: W/System.err(549):  at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
04-29 15:24:43.598: W/System.err(549):  at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
04-29 15:24:43.598: W/System.err(549):  at org.postgresql.Driver.makeConnection(Driver.java:393)
04-29 15:24:43.628: W/System.err(549):  at org.postgresql.Driver.connect(Driver.java:267)
04-29 15:24:43.628: W/System.err(549):  ... 18 more

04-29 15:24:43.898: E/System(549): Uncaught exception thrown by finalizer
04-29 15:24:43.898: E/System(549): java.lang.NullPointerException
04-29 15:24:43.898: E/System(549):  at org.postgresql.jdbc2.AbstractJdbc2Connection.close(AbstractJdbc2Connection.java:651)
04-29 15:24:43.898: E/System(549):  at org.postgresql.jdbc2.AbstractJdbc2Connection.finalize(AbstractJdbc2Connection.java:1003)
04-29 15:24:43.898: E/System(549):  at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
04-29 15:24:43.898: E/System(549):  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
04-29 15:24:43.898: E/System(549):  at java.lang.Thread.run(Thread.java:856)

我不知道出了什么问题。
我知道我应该使用网络服务(它更安全),但我只有 SQL 托管。
有人可以帮我吗?

4

1 回答 1

1

事件日志显示

Caused by: android.os.NetworkOnMainThreadException

您应该在自己的线程上移动代码或AsyncTask

您可以在此处阅读有关此异常的信息并了解它发生的原因。

于 2013-04-29T14:02:56.717 回答