【问题标题】:Android - AsyncTask or Thread while recovering data to SQLiteAndroid - 将数据恢复到 SQLite 时的 AsyncTask 或 Thread
【发布时间】:2015-11-11 12:23:05
【问题描述】:

我无法弄清楚如何进行这项工作。

我正在开发一个应用程序,在该应用程序中,我从在线 txt 下载数据,对其进行解析并将其添加到我的数据库中。这需要 +-30 秒,并且只实现了更新程序的一部分。

当我尝试使用线程或异步任务时,我在尝试将参数传递给正在更新数据库的 void 时遇到问题。

我怎样才能以好的方式实现它?

这是我的代码:

主要活动类:

public class Androideye_principal extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {


    //WhazzupUpdate.DownloadWhazzup(AllUsers, TotalConnections);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_androideye_principal);

    PilotsSQLiteHelper pdbh = new PilotsSQLiteHelper(this, "PilotsDB", null, 1);

    SQLiteDatabase dbPilots = pdbh.getWritableDatabase();

    Context context = getApplicationContext();


    String TotalConnections = new String();
    WhazzupUpdate.DownloadWhazzup(dbPilots, TotalConnections, context);   




    dbPilots.close();


    CharSequence text = "Total Users: " + TotalConnections;
    int duration = Toast.LENGTH_LONG;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

    [Continues with Buttons stuff and so on...]

解析器类:

public class WhazzupUpdate {

public static void DownloadWhazzup (SQLiteDatabase PilotsDB, String TotalConnections, Context context) {

    try {
        // Create a URL for the desired page
        URL url = new URL("This is my url/whazzup.txt");

        // Read all the text returned by the server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        for (int i = 0; i<=4; i++) {
        in.readLine(); }    // Go to the number of connections  
        TotalConnections = in.readLine();
        for (int i = 0; i<=3; i++) {
            in.readLine(); }    // Go to !CLIENTS
        PilotsDB.execSQL("DELETE FROM Pilots");

        while (((str = in.readLine()) != null) && !(in.readLine().contains("!AIRPORTS"))) {
            // str is one line of text; readLine() strips the newline character(s)

            String[] dataRow = str.split(":");

            if (str.contains("PILOT")) {
                ContentValues NewValue = new ContentValues();
                NewValue.put("VID", dataRow[1]);
                [More NewValue puts...]




            PilotsDB.insert("Pilots", null, NewValue);

            } //End IF Pilot


        } // End While
        in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

}}

如您所见,我在主要活动中调用 WhazzupUpdate.DownloadWhazzup 方法,此时一切都被冻结了,但不知道如何将其推导到另一个威胁并保留对数据库的引用等等...

希望任何人都可以帮助我。提前致谢。

【问题讨论】:

    标签: java android sqlite parsing android-asynctask


    【解决方案1】:

    ThreadAsyncTask 在这里就可以了。我更喜欢在我的大部分繁重工作中使用AsyncTask。您可以创建一个AsyncTask 并在doInBackground() 中工作,因为它在背景Thread 上工作。然后,如果需要,您可以在任何其他方法中更新您的 UI 元素。

    onPostExecute() 将在doInBackground() 传递结果后运行

    如果您需要在doInBackground() 操作期间通过调用publishProgress() 更新UIonProgressUpdate() 将运行。如果需要,您可以在这里显示ProgressBar

    onPreExecute() 将在您在doInBackground() 运行之前第一次调用任务时运行。

    使用ThreadAsyncTask 在后台线程中运行代码将使您的UI 在繁重的工作完成时空闲。

    Example of AsyncTask

    Using interface with AsyncTask to post data back yo UI

    AsyncTask Docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      相关资源
      最近更新 更多