2

I have finally managed to access postgres but the method is not according to the requirement. through first method i can connect but through second method i receive below error.

Here is my code.

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;
        }


    }

}

But i want to access it through this way. the CONNECT method is reference to a button in 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!-----");

        }
    }


}

Log output in the console.

    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!-----
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Omeed Totakhel
  • 487
  • 1
  • 10
  • 32
  • You do know that using JDBC connections on Android is not recommended, yes? – OneCricketeer Sep 12 '16 at 19:13
  • So what is the correct way to access database from android studio? – Omeed Totakhel Sep 12 '16 at 19:43
  • Firstly, I just want to say that Android Studio is only an IDE where you write code - it doesn't connect to the database. The recommended way is a REST API. A web server running on the same (or different) server as the database will run the connections. This provides safer, cleaner and more robust queries to be made against the database and prevents bloat in your app for the database code. – OneCricketeer Sep 12 '16 at 19:46
  • Your first code with the AsyncTask is the correct way. I don't know why you got rid of it in the second block of code. – OneCricketeer Sep 12 '16 at 19:51
  • Also, I think my initial answer was correct, which prompted you to edit and use `DriverManager.getConnection(url, "admin", "123");` and ultimately change the question. Please don't do that as it invalidates existing answers. Instead, you should accept them and make a new post. – OneCricketeer Sep 12 '16 at 19:53
  • actually at the first question the problem was with permission attribute. Which is aready solved. – Omeed Totakhel Sep 12 '16 at 19:58
  • Exactly, because your connection string was incorrect because it had no `?` between the table and the credentials – OneCricketeer Sep 12 '16 at 20:00

2 Answers2

0

Perhaps the problem is here

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

Make sure you have the internet permission such as:

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

Glad that OP fixed the permission problem.

And I believe the comments are clear enough for not using main thread to do this.

idler1515
  • 150
  • 3
  • 11
0

Your button's onClick handler (and in turn your Connect(View) method) is invoked on the main (UI) thread. As a result, your second code example is calling DriverManager.getConnection on the main thread which is a no-no in Android.

Your first code example works because you've offloaded the call to DriverManager.getConnection to a background thread (via AsyncTask.)

Michael Krause
  • 4,689
  • 1
  • 21
  • 25