0

I'm working on an Android app which has to connect to a Postgresql Database. For testing purposes, postgresql is running on my development machine.

Looking at my localhost interface with wireshark only shows a handshake between the android emulator and my development machine and no sign of a postgresql connection. The postgresql logs don't show any hint of such a connection either.

10.0.2.2 is the IP of my development machines localhost interface as seen by the android emulator.

Here is my code:

package com.example.kevin.servertest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.sql.*;

public class MainActivity extends AppCompatActivity {

private Button btnGet, btnPost;
private TextView txtGet;
private EditText numGet, txtPost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    try
    {
        Class.forName("org.postgresql.Driver");
    }
    catch (ClassNotFoundException e)
    {
        errorMsg("Postgresql Driver Not Found!");
        e.printStackTrace();
    }

    Connection connection = null;

    try
    {
        System.out.println("Establishing Connection...");
        connection = DriverManager.getConnection("jdbc:postgresql://10.0.2.2:5432/test", "dbuser", "dbpass");//Fails here
    }
    catch(SQLException e)
    {
        errorMsg("Connection Failed!");
        e.printStackTrace();
        return;
    }

    if (connection != null)
    {
        System.out.println("Connection Established!");
    }
    else
    {
        errorMsg("No Connection!");
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnGet = (Button) findViewById(R.id.button2);
    btnPost = (Button) findViewById(R.id.button);
    txtGet = (TextView) findViewById(R.id.textView);
    numGet = (EditText) findViewById(R.id.editText);
    txtPost = (EditText) findViewById(R.id.editText2);

    btnGet.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            System.out.println("btnGet");
            //Do get stuff
        }
    });

    btnPost.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            System.out.println("btnPost");
            //Do post stuff
        }
    });
}

void errorMsg(String msg)
{
    System.out.println("App-Error! " + msg);
}
}

My pg_hba.conf is the default pg_hba.conf.

Cœur
  • 37,241
  • 25
  • 195
  • 267

0 Answers0