Android SharedPreferences saved successfully but unable to get data problem

I am a Java code farmer, and I encountered a problem while exploring and developing Android by myself.

When using SharedPreferences to save parameters such as login token, it is found that the save is successful, and commit() also returns true, but it cannot be obtained. Here is the code:

SharedPreferences sp = getSharedPreferences("loginToken", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("token", userJson.getString("token"));
editor.putString("username", userJson.getString("username"));
editor.commit();
When getting it, it is found that only the first key with token has data. Then change the code to:
SharedPreferences sp = getSharedPreferences("loginToken", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("token", userJson.getString("token")).commit();
editor.putString("username", userJson.getString("username")).commit();

Each piece of data is committed () once.

Problem solved successfully.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325963749&siteId=291194637