我需要我的JSON数组保存到SharedPreferences,并在不同的活动日后访问。
这里是我的Json的例子:
Array
(
[status] => success
[reason] => success
[version] => 1
[total] => Array
(
[category_1] => 3
[category_2] => 3
[category_3] => 3
)
[title] => Array
(
[category_1] => Food
[category_2] => Drink
[category_3] => Dessert
)
[data] => Array
(
[category_1] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_2] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_3] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
( ...
)
)
)
MainActivity:
...
try {
JSONObject jObject = new JSONObject(result);
String status = jObject.getString("status");
if (status.equals("success")) {
// Log.e("status",status);
JSONObject jObject_data = new JSONObject(jObject.getString("data"));
JSONArray jArray_data_category_1 = new JSONArray(jObject_data.getString("category_1"));
JSONArray jArray_data_category_2 = new JSONArray(jObject_data.getString("category_2"));
//JSONArray jArray_data_category_3 = new JSONArray(jObject_data.getString("category_3"));
//Log.e("category_1 *****************************************", jArray_data_category_1.toString());
vc.insertCategory_1Pref(jArray_data_category_1.toString());
这是我的SharedPrefInput.java:
...
public class SharedPrefInput {
public static final String CATEGORY_1 = "CATEGORY_1";
public static final String CATEGORY_2 = "CATEGORY_2";
public static final String CATEGORY_3 = "CATEGORY_3";
...
这里是VarController
...
//category_1
public void insertCategory_1Pref(String s){
prefSetting = context.getSharedPreferences(SharedPrefInput.DEVICE_SETTING, Context.MODE_PRIVATE);
prefSettingEditor = prefSetting.edit();
prefSettingEditor.putString(SharedPrefInput.CATEGORY_1, s);
prefSettingEditor.commit();
}
public String readCategory_1Pref(){
prefSetting = context.getSharedPreferences(SharedPrefInput.DEVICE_SETTING, Context.MODE_PRIVATE);
return prefSetting.getString(SharedPrefInput.CATEGORY_1, "00");
}
...
这里是我试图访问它。 但它不工作。
try {
JSONArray jsonArray2 = new JSONArray(sharedCategory_1Pref.getString(CATEGORY_1, status));
for (int i = 0; i < jsonArray2.length(); i++) {
Log.d("your JSON Array", jsonArray2.getInt(i)+"");
Log.e("inside try", "inside try");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("inside Exception", "inside Exception");
}
这是日志当我尝试阅读SharePref:
08-12 09:22:34.757: W/System.err(871): at org.json.JSON.typeMismatch(JSON.java:96)
08-12 09:22:34.787: W/System.err(871): at org.json.JSONArray.getInt(JSONArray.java:357)
08-12 09:22:34.787: W/System.err(871): at com.example.brazilapps.MainActivity$1.onPostExecute(MainActivity.java:158)
08-12 09:22:34.787: W/System.err(871): at com.example.brazilapps.MainActivity$1.onPostExecute(MainActivity.java:1)
08-12 09:22:34.787: W/System.err(871): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-12 09:22:34.797: W/System.err(871): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-12 09:22:34.797: W/System.err(871): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-12 09:22:34.797: W/System.err(871): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 09:22:34.808: W/System.err(871): at android.os.Looper.loop(Looper.java:123)
08-12 09:22:34.808: W/System.err(871): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-12 09:22:34.818: W/System.err(871): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 09:22:34.827: W/System.err(871): at java.lang.reflect.Method.invoke(Method.java:507)
08-12 09:22:34.827: W/System.err(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-12 09:22:34.827: W/System.err(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-12 09:22:34.867: W/System.err(871): at dalvik.system.NativeStart.main(Native Method)
08-12 09:22:34.867: E/inside Exception(871): inside Exception