gpt4 book ai didi

java - 访问 postgresql 数据库时出现 NetworkOnMainThreadException

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

好吧,尝试使用 Android 访问 postgresql 数据库时,我得到了以下非常令人惊讶的异常:

    org.postgresql.util.PSQLException: Something unusual has occurred to cause the driver to fail. Please report this exception.        at org.postgresql.Driver.connect(Driver.java:305)        at java.sql.DriverManager.getConnection(DriverManager.java:179)        at java.sql.DriverManager.getConnection(DriverManager.java:213)        at org.pbdevelopement.cineapp.NetReader.getConnection(NetReader.java:42)        at org.pbdevelopement.cineapp.MainActivity.getPersonalMovies(MainActivity.java:242)        at org.pbdevelopement.cineapp.MainActivity.onCreate(MainActivity.java:115)        at android.app.Activity.performCreate(Activity.java:6034)        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)        at android.app.ActivityThread.access$800(ActivityThread.java:156)        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)        at android.os.Handler.dispatchMessage(Handler.java:102)        at android.os.Looper.loop(Looper.java:211)        at android.app.ActivityThread.main(ActivityThread.java:5373)        at java.lang.reflect.Method.invoke(Native Method)        at java.lang.reflect.Method.invoke(Method.java:372)        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)    Caused by: android.os.NetworkOnMainThreadException        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1151)        at java.net.InetAddress.lookupHostByName(InetAddress.java:418)        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)        at java.net.InetAddress.getByName(InetAddress.java:305)        at java.net.InetSocketAddress.(InetSocketAddress.java:105)        at java.net.InetSocketAddress.(InetSocketAddress.java:90)        at org.postgresql.core.PGStream.(PGStream.java:61)        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:129)        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)        at org.postgresql.jdbc.PgConnection.(PgConnection.java:159)        at org.postgresql.Driver.makeConnection(Driver.java:415)        at org.postgresql.Driver.connect(Driver.java:283)        ... 18 more

我不知道如何解决这个问题,也不知道该向何处报告...有人有任何想法吗?

最佳答案

您似乎正试图通过应用程序主线程上的网络操作来执行您的数据库请求。这将引发异常 android.os.NetworkOnMainThreadException,因为 Android 中的每个网络操作都必须在自己的线程中启动。尝试在 AsyncTask 中执行您的数据库请求。

AsyncTask

关于java - 访问 postgresql 数据库时出现 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509630/

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