我正在做一个类似的项目。我请求对数据库进行更新,根据所需的更新量,它要么发送一个全新的数据库,要么发送一系列 SQL 语句。如果数据库可用,我强烈建议将数据库保留在 sdcard 上,尤其是在您下载的数据库大小可变且大小超过几千字节的情况下。
我正在执行以下操作来获取文件(根据您的喜好进行修改,因为您可能需要在 SOAP 响应中处理 XML,也忽略进度的内容。)
fileStream = new FileOutputStream(new File(I-build-my-filename-stuff-above-here));
/* fileData is a byte[8192] */
while((i = httpResponse.read(fileData)) > -1) {
fileStream.write(fileData, 0, i);
position += i;
if (item.getUpdateType() == UpdateItem.FULL_FILE) {
progress = position / item.getSize();
} else {
progress = position / (item.getSize() * 2);
}
item.setProgress(progress);
} // end loop through getting http response stream
fileStream.close();
httpResponse.close();
然后我执行以下操作来访问数据库。由于这些数据库也用于 iPhone 应用程序,因此它们没有特定于 Android 的表,因此需要 SQLiteDatabase.NO_LOCALIZED_COLLATORS 标志来访问数据库。请注意,我的 Database 类扩展了 SQLiteOpenHelper 类:
public Database(Context context, String fileName, String title) {
super(context, fileName, null, 1);
this.context = context;
this.title = title;
this.fileName = fileName;
if (fileName != null && fileName.contains("/sdcard")) {
db = SQLiteDatabase.openDatabase(fileName, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
} else {
db = getWritableDatabase();
}
}