Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java - Postgresql not connecting to android using JDBC throwing org.postgresql.util.PSQLException: The connection attempt failed

I am developing an android application for which I need to connect to Postgresql database, I installed it 9.3 version, and checked in PGAdminIII it is connecting. I also created a java project in eclipse just for testing JDBC it is connecting successfully but when I am trying to connet to Postgresql from Android project it is throwing error: org.postgresql.util.PSQLException: The connection attempt failed. Here is the code Which I written in MainActivity.java

@Override
protected Void doInBackground(Void... params) {
    try {

          Class.forName("org.postgresql.Driver");
          Connection  conn = DriverManager.getConnection("jdbc:postgresql://192.168.43.207:5432/testdb", "postgres", "password");
          System.out.println("connection success");
          conn.close() ;
        } 
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

and pg_hba.conf

IPv4 local connections:

host all all 192.168.43.207 trust

IPv6 local connections:

host all all ::1/128 trust

postgresql.conf

listen_addresses = '*'

Note: If I am changing ip address 192.168.43.207 with localhost/127.0.0.1 it is throwing error- org.postgresql.util.psqlexception connection refused. check that the hostname and port are correct I have googled about it and also seen posts on Stackoverflow but nothing works. I am using windows7 please provide solution according to it. please help me as I am fighting it for last two days but nothing works.

Edit: when I run these commands from CMD in windows I got following outout

C:Windowssystem32>sc query postgresql-9.3

SERVICE_NAME: postgresql-9.3
    TYPE               : 10  WIN32_OWN_PROCESS
    STATE              : 4  RUNNING
                            (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

C:Windowssystem32>netstat -a | findstr 5432
  TCP    0.0.0.0:5432           -PC:0           LISTENING
  TCP    [::]:5432              -PC:0           LISTENING
  TCP    [::1]:5432             -PC:49573       ESTABLISHED
  TCP    [::1]:5432             -PC:49574       ESTABLISHED
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem is that 127.0.0.1 on the Android goes to the virtual Android device and not to your machine hosting the PostgreSQL database. Referencing the Android emulator documentation for networking:

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

And below that, there is a table for the virtual router:

Android virtual router network

And further down in a section about using 127.0.0.1 (emphasis mine):

Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead.

You should use 10.0.2.2 rather than 127.0.0.1 if you want to connect to the development machine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...