当前位置: 首页 > 知识库问答 >
问题:

在android后台加载数据

姬俊驰
2023-03-14

我正在尝试执行某些操作,例如创建一个表,并在应用程序的初始启动时插入720行,这需要一些时间,所以我决定在后台运行它。请参见下面的代码,

主要活动。Java语言

DatabaseHandler dbhandler = new DatabaseHandler(this);
this.progressDialog = ProgressDialog.show(this, "Please wait...",
            "Downloading Passengers Information...", true, false);

new DownloadTask().execute();


private class DownloadTask extends AsyncTask<String, Void, Object> {

    @Override
    protected Object doInBackground(String... args) {

        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < 11; j++) {
                value = jedis.hget("passengerInfo",
                        String.valueOf(i + (char) (asciiA) + j)); //this will get the data from hash table passengerInfo created in Redis
                if (value != null) {
                    addSeat = String.valueOf(i + (char) (asciiA) + j);
                    paxInfo = value.split(",");
                    passName = paxInfo[0];
                    passAge = Integer.parseInt(paxInfo[1]);
                    passGender = Integer.parseInt(paxInfo[2]);
                    passFoodPref = paxInfo[3];

                    dbhandler.addPassengerInfo(new PassengerInfo(addSeat,
                            passName, passAge, passGender, passFoodPref));
                }

                else {
                    addSeat = String.valueOf(i + (char) (asciiA) + j);
                    passName = "NA";
                    passAge = 0;
                    passGender = -1;
                    passFoodPref = "NA";

                    dbhandler.addPassengerInfo(new PassengerInfo(addSeat,
                            passName, passAge, passGender, passFoodPref));
                }

            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        // Pass the result data back to the main activity
        MainActivity.this.data = result;

        if (MainActivity.this.progressDialog != null) {
            MainActivity.this.progressDialog.dismiss();
        }
    }
}

当我运行它时,它显示logcat中的以下错误,

01-23 21:29:06.206: E/AndroidRuntime(704): FATAL EXCEPTION: AsyncTask #1
01-23 21:29:06.206: E/AndroidRuntime(704): java.lang.RuntimeException: An error occured while executing doInBackground()

请帮忙!

仅供参考:整个Logcat

01-23 21:53:15.807: E/AndroidRuntime(742):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.lang.Thread.run(Thread.java:1019)
01-23 21:53:15.807: E/AndroidRuntime(742): Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Connection timed out
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.Connection.connect(Connection.java:134)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:69)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.Connection.sendCommand(Connection.java:79)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.BinaryClient.hget(BinaryClient.java:212)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.Client.hget(Client.java:138)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.Jedis.hget(Jedis.java:680)
01-23 21:53:15.807: E/AndroidRuntime(742):  at android.jedisconnectiontest.com.MainActivity$DownloadTask.doInBackground(MainActivity.java:118)
01-23 21:53:15.807: E/AndroidRuntime(742):  at android.jedisconnectiontest.com.MainActivity$DownloadTask.doInBackground(MainActivity.java:1)
01-23 21:53:15.807: E/AndroidRuntime(742):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-23 21:53:15.807: E/AndroidRuntime(742):  ... 4 more
01-23 21:53:15.807: E/AndroidRuntime(742): Caused by: java.net.SocketTimeoutException: Connection timed out
01-23 21:53:15.807: E/AndroidRuntime(742):  at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
01-23 21:53:15.807: E/AndroidRuntime(742):  at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
01-23 21:53:15.807: E/AndroidRuntime(742):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
01-23 21:53:15.807: E/AndroidRuntime(742):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
01-23 21:53:15.807: E/AndroidRuntime(742):  at java.net.Socket.connect(Socket.java:983)
01-23 21:53:15.807: E/AndroidRuntime(742):  at redis.clients.jedis.Connection.connect(Connection.java:129)
01-23 21:53:15.807: E/AndroidRuntime(742):  ... 13 more
01-23 21:53:17.737: E/WindowManager(742): Activity android.jedisconnectiontest.com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40523ac8 that was originally added here
01-23 21:53:17.737: E/WindowManager(742): android.view.WindowLeaked: Activity android.jedisconnectiontest.com.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40523ac8 that was originally added here
01-23 21:53:17.737: E/WindowManager(742):   at android.view.ViewRoot.(ViewRoot.java:258)
01-23 21:53:17.737: E/WindowManager(742):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-23 21:53:17.737: E/WindowManager(742):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-23 21:53:17.737: E/WindowManager(742):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.Dialog.show(Dialog.java:241)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ProgressDialog.show(ProgressDialog.java:107)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ProgressDialog.show(ProgressDialog.java:95)
01-23 21:53:17.737: E/WindowManager(742):   at android.jedisconnectiontest.com.MainActivity.onCreate(MainActivity.java:56)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-23 21:53:17.737: E/WindowManager(742):   at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 21:53:17.737: E/WindowManager(742):   at android.os.Looper.loop(Looper.java:123)
01-23 21:53:17.737: E/WindowManager(742):   at android.app.ActivityThread.main(ActivityThread.java:3683)
01-23 21:53:17.737: E/WindowManager(742):   at java.lang.reflect.Method.invokeNative(Native Method)
01-23 21:53:17.737: E/WindowManager(742):   at java.lang.reflect.Method.invoke(Method.java:507)
01-23 21:53:17.737: E/WindowManager(742):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-23 21:53:17.737: E/WindowManager(742):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-23 21:53:17.737: E/WindowManager(742):   at dalvik.system.NativeStart.main(Native Method)

共有1个答案

郭均
2023-03-14
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Connection timed out
Caused by: java.net.SocketTimeoutException: Connection timed out

此错误表示服务器在给定的TimeOut值(正常约10秒)之间没有应答,您的服务器是否已关闭?还是您拼错了服务器url?

 类似资料:
  • 我目前正在尝试为我的程序创建一个启动屏幕,因为启动需要一些时间。问题是创建GUI需要一段时间(创建对话、更新表格等)。而且我不能将GUI创建移动到后台线程(如“Task”类),因为我会得到“Not on FXApplication thread”异常。我尝试使用: 以及任务的“调用”方法: 当我在Swing中编写程序时,我可以在EventDispatchThread上显示和更新Splash屏幕,而

  • 我正在开发一个Android应用程序,有一个不断重复的后台进程。 从设备启动的那一刻起,它应该每分钟加载一个网页上的数据。它使用XmlPullParser和一个简单的URL InputStream。它只有10KB,所以没有那么密集。我相信这种任务叫做延期。一旦用户打开应用程序,流程加载的信息必须可以访问活动。后台进程也需要设置,以便在数据显示某些结果时发出通知。 在Android中似乎有多种方法来

  • 我原以为这是一个简单的问题,但我很难找到答案。我有一个与JavaFX场景对象关联的ImageView对象,我想从磁盘加载大图像,并使用ImageView一个接一个地显示它们。我一直在努力寻找一种好方法来反复检查图像对象,当它在后台加载完成时,将其设置为ImageView,然后开始加载新的图像对象。我提出的代码(如下)有时有效,有时无效。我很确定我在JavaFX和线程方面遇到了问题。它有时加载第一个

  • 任何帮助都将得到高度赞赏。求求你了。

  • 我正在创建一个应用程序,其中我从服务器下载一些数据。在后台运行时,我希望连接应该继续运行,以便可以下载数据。我知道在app里有方法 当应用程序进入后台时调用。但由于连接是在viewController中创建的,如何在appDelegate中管理它 还有其他方法可以做到这一点吗?我已经通过了这个链接,但是有一个简单的实现方法吗?

  • 我最初的fxml(例如)有很多功能,因此完全加载需要大量时间。因此,为了避免程序启动和fxml加载之间的时间间隔,我引入了另外一个fxml()和一个gif图像,该图像应该在加载主fxml时出现。问题是我的加载器中的gif图像。fxml不会移动,就像程序中的挂起一样,直到home.fxml被完全加载。为了避免这种情况,我将home.fxml加载移到一个线程中,如下代码所示。 但是在这段代码之后,我的