There is such a task: to get a database file (in .db format) from the server and to overwrite the old file with a new one.

I try this:

@Override public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, File file) { if (statusCode != 200) { Toast.makeText(SettingsActivity.this, getResources().getText(R.string.error), Toast.LENGTH_SHORT).show(); return; } Log.v(TAG, "response - success - " + file); try { FileInputStream input = new FileInputStream(file); String outputFile = String.valueOf(getApplicationContext().getDatabasePath("name_db")); OutputStream output = new FileOutputStream(outputFile); byte[] buffer = new byte[input.available()]; int i; while ((i = input.read(buffer)) > 0) { output.write(buffer, 0, i); } output.flush(); output.close(); input.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } pd.cancel(); } 

But it does not work. How best to implement this?

Just in case, the code to send the database file to the server:

  RequestParams params_ = new RequestParams(); AsyncHttpClient client = new AsyncHttpClient(true, 80, 443); Context context = getApplicationContext(); File dbFile = new File(String.valueOf(context.getDatabasePath("name_db"))); try { params_.put("db", dbFile); } catch(FileNotFoundException e) { Log.v(TAG, "error - file not found"); } client.post("example.com", params_, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject json) { Log.v(TAG, "response -- " + json); try { if (0 == json.getInt("status")) { Toast.makeText(SettingsActivity.this, getResources().getText(R.string.successfully), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(SettingsActivity.this, getResources().getText(R.string.error), Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } pd.cancel(); } }); 

PS I tried it like this:

 (new DB(getApplicationContext())).close(); File aFile = file; String path = String.valueOf(getApplicationContext().getDatabasePath("targ‌​ets_db")) + ".db"; if (aFile.renameTo(new File(path))) { Log.v(TAG, "File is moved successful!"); } 

In the logs it says that the file has been moved, but however the database file has not changed to the desired one (backup).

  • Are you sure you want everyone to receive a new file? And not to receive data for some period and temporarily cache them locally? - Kirill
  • @AstendSanferion, tried - did not help - Amandi
  • @KirillMatasov, the essence is this - I made backup bd to my server, then, if something went wrong, I could restore the bd from backup on the server. - Amandi
  • What does it mean does not work? In LogCat looked, there are errors? If the base at the time of dubbing is already open, you need to close. - woesss

0