【问题标题】:Downloading data from internet - pay attention to what?从互联网下载数据 - 注意什么?
【发布时间】:2013-03-05 12:19:26
【问题描述】:

在我的应用程序中,我使用 xml 阅读器从网站下载数据。之后,我用他们建立了一个 sqlite 数据库。一切正常,但我担心下载过程中会发生某些事情(连接中断),导致数据库不完整。

有没有人有类似条件的列表,说明我需要为save 下载做什么? (例如:在下载开始前检查连接)

【问题讨论】:

    标签: android download internet-connection


    【解决方案1】:

    在解析 XML 并插入数据库时​​,您将执行以下代码:

    db.beginTransaction();
    try {
    
    
     // here you parse and insert onto the DB.
     // if any exception is reached (the XML reader connection was dropped or whatever)
     // it will catch the exception and end the transaction without making it successful
     // and SQLite will rollback all the actions you've done here.
    
    
    
      db.setTransactionSuccessful();
    } catch(Exception e){
      Log.e("MyTag", "Transaction fail. " +
                      e.getClass().getSimpleName() + " - " + 
                      e.getMessage());
    }finally {
      db.endTransaction();
    }
    

    【讨论】:

      【解决方案2】:

      你可以在填充完所有数据库后设置一个变量。

      当您访问该数据库时检查该变量是否设置了该变量?

      如果未设置,则再次请求下载为Data is not completed download Please download again.

      正如您在连接中断时建议您自己检查该变量是否在中断之前设置它意味着所有数据已下载,否则它不是。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多