AndroidからPostgreSQLへ接続

2,106 views
Skip to first unread message

TaKa

unread,
Dec 20, 2014, 3:39:23 AM12/20/14
to android-g...@googlegroups.com
こんにちは。

Android端末から、ローカルのPostgreSQLサーバーに接続しようとするのですが、
以下のエラーが出ます。
Something unusual has occurred to cause the driver to fail. Please report this exception.

ソースは
public class JDBCTest {
  public static void main(String [] args) {
    try {


      try {
         Class. forName("org.postgresql.Driver" );
         Connection con =
             DriverManager.getConnection ("jdbc:postgresql://localhost:5432/postgres",
                                         "postgres",
                                         "postgres");


        } catch (Exception e) {
           e. printStackTrace();
        }

//以下略

ドライバのロードはできているのですが、getConnectionでエラーが発生します。
localhostを10.0.0.2にしても同じエラーでした。
mainメソッドはAndroidのメインアクティビティから呼び出しています。

postgreSQLのpg_hba.confのIPアドレスの許可も行っております。
Androidを介さず、Java単体で動作した場合は、エラーなく動作します。
データベースの中身も問題なく見れます。なのでホスト名、ユーザーID、パスワード間違っていないです。

しかしAndroid上で動作させると上記のエラーになります。
Android側の問題でしょうか?それともPostgreSQL側でしょうか?




Hirokazu Fukami

unread,
Dec 20, 2014, 4:06:42 AM12/20/14
to android-g...@googlegroups.com
こんにちはfkmです。

Android端末内でPostgreSQLサーバーを動かしているのでしょうか?

--
このメールは Google グループのグループ「日本Androidの会」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには android-group-j...@googlegroups.com にメールを送信してください。
このグループに投稿するには android-g...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-group-japan からこのグループにアクセスしてください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。

TaKa

unread,
Dec 20, 2014, 4:13:13 AM12/20/14
to android-g...@googlegroups.com
こんにちは

fkmさん

紛らわしくてすみません
PostgreSQLは同じ自宅のLAN内にある別サーバーです。
localhostの部分をそのサーバーのIPにしてもダメでした。


追記ですが、Androidの端末から、そのPosqgreSQLのサーバーにPingは通ります。

TaKa

unread,
Dec 20, 2014, 7:32:13 PM12/20/14
to android-g...@googlegroups.com
こんにちは
自己解決しました。
NetworkOnMainThreadExceptionがでていました。
メインスレッドでネットワーク接続してはいけないのですね。
AsyncTaskを継承したクラスを作って、そこから接続したらOKでした。



Reply all
Reply to author
Forward
0 new messages