S
S
svirepui2017-09-03 15:09:25
Java
svirepui, 2017-09-03 15:09:25

How to overwrite a db file?

There is such a task: to get the database file from the server (in .db format) and overwrite the old file with the new one.
I try like 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 doesn't work. What is the best way 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();
             }
       });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question