将 SharedPreference 中的对象保存为 Json 和关键更改
Saving object in SharedPreference as Json and Key Changes
我的应用程序在 PlayStore 中。当我使用新导出的 apk 更新我的应用程序时,SharedPreference 密钥正在更改。您可以通过查看下面给出的 json 字符串来理解它。
我正在按照下面给出的共享首选项保存一个对象。
正在 SharedPreference 中保存用户详细信息。
Gson gson = new Gson();
String json = gson.toJson(user);
prefsEditor.putString("user", json);
prefsEditor.commit();
从 sharedPrefernce 获取详细信息
Gson gson = new Gson();
String json = myPref.getString("user", null);
Log.v(MainActivity.TAG, "Json == "+json);
user = gson.fromJson(json, User.class);
请看一下我正在打印的jsons,键值改为'a','b'等
当我运行通常JSON是Json == {"accessToken":"12345678","country":"India","countryCode":"91","email":"","firstName":"Aneesh","lastName":"Kamalone","nickName":"Kamalone","notificationId":"","phoneNumber":"******","profilePic":"","uniqueId":"","userId":"*****","userLatitude":14.9,"userLogitude":79.6}
当我用以前的版本更新时 json 是 Json == {"a":"****","b":"Aneesh","c":"Kamalone","d":"Kamalone","e":"**********","f":"","g":"APA91bElQulddjH6xSR0WH8nfBGcFIbn-sRe-ChkhIx1c6Il05_wwGrTLm1QDNsr6TtaG1bIrICmXKhK1I8ZVzo0cqvwPWSj4NrxJKyzTsdPpNlizmGMaJfMWj_rjteP6yNoRFeDAZYfTCyhx-7WUAmTT2rX0q1S6Q","h":"","i":"12345678","j":"India","k":"91","l":14.9,"m":79.6,"n":""}
你似乎在使用 proguard,它混淆了你的 User
class。
Gson
默认使用字段名作为键。所以我建议使用 @SerializedName
注释来正确 json 映射
这是我的错误。proguard 在我导出 APK 时未启用。
我的应用程序在 PlayStore 中。当我使用新导出的 apk 更新我的应用程序时,SharedPreference 密钥正在更改。您可以通过查看下面给出的 json 字符串来理解它。
我正在按照下面给出的共享首选项保存一个对象。
正在 SharedPreference 中保存用户详细信息。
Gson gson = new Gson(); String json = gson.toJson(user); prefsEditor.putString("user", json); prefsEditor.commit();
从 sharedPrefernce 获取详细信息
Gson gson = new Gson(); String json = myPref.getString("user", null); Log.v(MainActivity.TAG, "Json == "+json); user = gson.fromJson(json, User.class);
请看一下我正在打印的jsons,键值改为'a','b'等
当我运行通常JSON是
Json == {"accessToken":"12345678","country":"India","countryCode":"91","email":"","firstName":"Aneesh","lastName":"Kamalone","nickName":"Kamalone","notificationId":"","phoneNumber":"******","profilePic":"","uniqueId":"","userId":"*****","userLatitude":14.9,"userLogitude":79.6}
当我用以前的版本更新时 json 是
Json == {"a":"****","b":"Aneesh","c":"Kamalone","d":"Kamalone","e":"**********","f":"","g":"APA91bElQulddjH6xSR0WH8nfBGcFIbn-sRe-ChkhIx1c6Il05_wwGrTLm1QDNsr6TtaG1bIrICmXKhK1I8ZVzo0cqvwPWSj4NrxJKyzTsdPpNlizmGMaJfMWj_rjteP6yNoRFeDAZYfTCyhx-7WUAmTT2rX0q1S6Q","h":"","i":"12345678","j":"India","k":"91","l":14.9,"m":79.6,"n":""}
你似乎在使用 proguard,它混淆了你的 User
class。
Gson
默认使用字段名作为键。所以我建议使用 @SerializedName
注释来正确 json 映射
这是我的错误。proguard 在我导出 APK 时未启用。