我开发了一个带有本地SQLite数据库的Android应用程序。 现在我正在尝试将此SQLite .db数据库与具有简单文件替换的服务器同步 - 只需从服务器下载.db并替换当前的.db。
我已经使用true / false创建了一个JSON响应,只是为了检查在线.db文件是否比本地.db文件更新,所以我的问题是:如何从www.domain.com/获取.db文件sqlite.db并从我的应用程序替换现有的Android sqlite .db文件。
答案 0 :(得分:4)
尝试使用此代码检查数据库版本:
class CheckDb extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
c = jParser.getJSONFromUrl(url);
try {
// Getting JSON Array;
// Storing JSON item in a Variable
final String id = c.getString(verzija).toString().trim();
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
//Importing TextView
final TextView json = (TextView)findViewById(R.id.textView);
//Set JSON Data in TextView
try {
json.setText(c.getString(verzija).toString().trim());
String db_version = c.getString(verzija).toString().trim();
String current_version = "3";
if(!db_version.equals(current_version)) {
new Thread(new Runnable() {
public void run() {
downloadFile();
}
}).start();
}
else {
dialog.hide();
Toast.makeText(MainActivity.this, "You have valid version of database", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(s);
}
}
这用于下载新版本并替换现有版本:
void downloadFile(){
try {
URL url = new URL(dwnload_file_path);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//connect
urlConnection.connect();
//set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, to save the downloaded file
File file = new File(SDCardRoot,"download_db.db");
FileOutputStream fileOutput = new FileOutputStream(file);
//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
runOnUiThread(new Runnable() {
public void run() {
pb.setMax(totalSize);
}
});
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
runOnUiThread(new Runnable() {
public void run() {
pb.setProgress(downloadedSize);
float per = ((float)downloadedSize/totalSize) * 100;
cur_val.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
}
});
}
//close the output stream when complete //
fileOutput.close();
runOnUiThread(new Runnable() {
public void run() {
// pb.dismiss(); // if you want close it..
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException " + e);
e.printStackTrace();
}
catch (final Exception e) {
showError("Error : Please check your internet connection " + e);
}
}
此外,您需要更改要保存文件的路径,并为AndroidManifest
添加互联网权限。