在android中的后台加载数据

时间:2013-01-23 16:29:41

标签: android android-asynctask

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

MainActivity.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 个答案:

答案 0 :(得分:0)

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Connection timed out
Caused by: java.net.SocketTimeoutException: Connection timed out

此错误表示服务器未在给定的TimeOut值(正常约10秒)之间应答,您的服务器是否已关闭?或者你拼错了服务器网址?