通过JDBC连接到PostgreSQL - NetworkOnMainThreadException

时间:2016-09-12 17:58:09

标签: android postgresql jdbc

我终于设法访问postgres但该方法不符合要求。通过第一种方法,我可以连接,但通过第二种方法,我收到以下错误。

这是我的代码。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button login = (Button) findViewById(R.id.Connect);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //new JSONTask().execute("file:///C:/Users/intel/Desktop/details.json");
                System.out.println("Connection Start");
                new PostgreSqlJDBC().execute();
            }
        });
    }

    public class PostgreSqlJDBC extends AsyncTask<Void, Void, Void> {
        @Override
        public Void doInBackground(Void... params) {
            Connection conexion = null;
            System.out.println("Connection Intitializing");
            try {
                Class.forName("org.postgresql.Driver");
                String url="jdbc:postgresql://192.168.0.5:5432/Taxi";
                conexion = DriverManager.getConnection(url, "admin", "123");
                System.out.println("Connection Successfull");
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(e.getMessage());
                System.err.println("Error: Cant connect!");

                System.err.println("----- PostgreSQL query ends correctly!-----");
                return null;
            }
            return null;
        }


    }

}

但我希望通过这种方式访问​​它。 CONNECT方法引用xml中的按钮。

 public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void Connect(View v) {
        Connection conexion = null;
        System.out.println("Connection Intitializing");
        try {
            Class.forName("org.postgresql.Driver");
            String url="jdbc:postgresql://192.168.0.5:5432/Taxi";
            conexion = DriverManager.getConnection(url, "admin", "123");
            System.out.println("Connection Successfull");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
            System.err.println("Error: Cant connect!");

            System.err.println("----- PostgreSQL query ends correctly!-----");

        }
    }


}

在控制台中记录输出。

    09-13 00:08:10.990    6948-6948/? I/System.out﹕ Connection Intitializing
09-13 00:08:10.993    1233-1294/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
09-13 00:08:11.079    6948-6948/? W/System.err﹕ org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at org.postgresql.Driver.connect(Driver.java:295)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:213)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at com.echotaxicab.MainActivity.Connect(MainActivity.java:42)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View$1.onClick(View.java:4015)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View.performClick(View.java:4780)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.view.View$PerformClick.run(View.java:19866)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
09-13 00:08:11.079    6948-6948/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ Caused by: android.os.NetworkOnMainThreadException
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
09-13 00:08:11.080    6948-6948/? W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.PGStream.<init>(PGStream.java:64)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:147)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:218)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.Driver.makeConnection(Driver.java:407)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ at org.postgresql.Driver.connect(Driver.java:275)
09-13 00:08:11.081    6948-6948/? W/System.err﹕ ... 16 more
09-13 00:08:11.081    6948-6948/? W/System.err﹕ Something unusual has occurred to cause the driver to fail. Please report this exception.
09-13 00:08:11.081    6948-6948/? W/System.err﹕ Error: Cant connect!
09-13 00:08:11.081    6948-6948/? W/System.err﹕ ----- PostgreSQL query ends correctly!-----

2 个答案:

答案 0 :(得分:0)

问题可能就在这里

android.system.ErrnoException: socket failed: EACCES (Permission denied)

确保您拥有互联网权限,例如:

<uses-permission android:name="android.permission.INTERNET"/>

很高兴OP解决了许可问题。

我相信这些评论足够清楚,因为不使用主线程来做到这一点。

答案 1 :(得分:0)

在主(UI)线程上调用您的按钮的onClick处理程序(以及您的Connect(View)方法)。因此,您的第二个代码示例是在主线程上调用DriverManager.getConnection,这在Android中是禁止的。

您的第一个代码示例有效,因为您已将对DriverManager.getConnection的调用卸载到后台线程(通过AsyncTask。)