0

I try to save SharedPreferences in mysql and restore it. I use the following code to store the SharedPreferences:

 private boolean saveSharedPreferencesToFile(File dst) {
        boolean res = false;
        ObjectOutputStream output = null;
        try {
            output = new ObjectOutputStream(new FileOutputStream(dst));
            SharedPreferences pref = getSharedPreferences("AnimeSAShardPref", MODE_PRIVATE);
            output.writeObject(pref.getAll());
            res = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (output != null) {
                    output.flush();
                    output.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return res;
    }

After I create the file, I read the file and upload it to mysql and it's ok. But when I want to get back the data from mysql to make it file so I can use:

 @SuppressWarnings({ "unchecked" })
    private boolean loadSharedPreferencesFromFile(File src) {
        boolean res = false;
        ObjectInputStream input = null;
        try {
            input = new ObjectInputStream(new FileInputStream(src));
            SharedPreferences.Editor prefEdit = getSharedPreferences("sssss", MODE_PRIVATE).edit();
            prefEdit.clear();
            Map<String, ?> entries = (Map<String, ?>) input.readObject();
            for (Map.Entry<String, ?> entry : entries.entrySet()) {
                Object v = entry.getValue();
                String key = entry.getKey();

                if (v instanceof Boolean)
                    prefEdit.putBoolean(key, ((Boolean) v));
                else if (v instanceof Float)
                    prefEdit.putFloat(key, ((Float) v));
                else if (v instanceof Integer)
                    prefEdit.putInt(key, ((Integer) v));
                else if (v instanceof Long)
                    prefEdit.putLong(key, ((Long) v));
                else if (v instanceof String)
                    prefEdit.putString(key, ((String) v));
            }
            prefEdit.apply();
            res = true;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return res;
    }

I get this error:

    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ java.io.StreamCorruptedException
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2067)
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at com.test.LoginActivity.loadSharedPreferencesFromFile(LoginActivity.java:347)
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at com.test.LoginActivity.access$300(LoginActivity.java:57)
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at com.test.LoginActivity$DownloadFileFromURL.onPostExecute(LoginActivity.java:337)
    04-30 10:45:00.910  24101-24101/com.test W/System.err﹕ at com.test.LoginActivity$DownloadFileFromURL.onPostExecute(LoginActivity.java:303)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5593)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    04-30 10:45:00.920  24101-24101/com.test W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
erakitin
  • 11,437
  • 5
  • 44
  • 49
somone
  • 1
  • 1
  • 1
    What are you trying to achieve here? Why do you want to save the sharedPreferences in a databse? – k3v1n4ud3 Apr 30 '15 at 08:11
  • you define a method for saving sharedpreferences into a file, that gives me the impression that you don't know that they are already stored in a file. please look at this [question](http://stackoverflow.com/questions/2566430/sharedpreferences-file) – Adeeb Apr 30 '15 at 08:19
  • i want save all app Settings in database so when user login he will restore it – somone Apr 30 '15 at 08:59
  • Adeeb i know , this is not the problem , the problem is when i upload it database and i load it to SharedPreferences i got the error above . – somone Apr 30 '15 at 09:01
  • Out of the topic of this question, but do you mean `SQLite` instead of `MySQL`? – Sajib Acharya Nov 24 '15 at 17:50

0 Answers0