2013-04-29 119 views
2

我在Android上遇到PostgreSQL JDBC驅動程序問題。
我使用postgresql-9.2-1002.jdbc4.jar JDBC驅動程序。PostgreSQL JDBC和Android - 空指針異常

這裏是我的代碼:

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託管。
有人可以幫我嗎?

+0

通常我建議避免從android使用JDBC。運行應用服務器,爲您的應用公開REST API調用,然後從android應用中調用它。請參閱http://stackoverflow.com/q/15853367/398670 – 2014-01-31 00:47:04

回答

1

事件日誌顯示

Caused by: android.os.NetworkOnMainThreadException 

你應該將代碼在自己的線程或AsyncTask

你可以閱讀有關此異常here並理解爲什麼它會發生。

+0

感謝您的回答。我會嘗試使用AsyncTask。 – simon 2013-04-29 14:26:44