gpt4 book ai didi

android - 通过 JDBC 连接到 PostgreSQL - NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-29 12:25:39 30 4
gpt4 key购买 nike

<分区>

我终于成功访问了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!-----

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com