【问题标题】:I have parsed the JSON Data in a Gridview and now I want to make it available offline我已经在 Gridview 中解析了 JSON 数据,现在我想让它离线使用
【发布时间】:2016-09-08 07:28:16
【问题描述】:

有没有办法将 JSON 数据保存在手机上,以便在手机离线时查看数据?

缓存数据的最佳选择是什么? SharedPreferences 或 SQLite 数据库

这是我用来解析 JSON 的代码:

   if (InternetConnection.checkConnection(getApplicationContext())) 
        new GetDataTask().execute();



        class GetDataTask extends AsyncTask<Void, Void, Void> {

            private static final String KEY_ICONURL ="http://droid.hol.es/images/"; ;
            ProgressDialog dialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                dialog = new ProgressDialog(Exit_View_Activity.this);

                dialog.show();
            }

            @Nullable
            @Override
            protected Void doInBackground(Void... params) {

                 jsonObject = JSONParser.getDataFromWeb();

                try {

                    if (jsonObject != null) {

                        if(jsonObject.length() > 0) {

                            JSONArray array = jsonObject.getJSONArray(Keys.KEY_INFO);

                            int lenArray = array.length();
                            if(lenArray > 0) {
                                for(int jIndex = 0; jIndex < lenArray; jIndex++) {                                     
                                    MyDataModel model = new MyDataModel();


                                    JSONObject innerObject = array.getJSONObject(jIndex);
                                    String name = innerObject.getString(Keys.KEY_NAME);
                                    String viewtype = innerObject.getString(Keys.KEY_VIEWTYPE);
                                    String image = innerObject.getString(Keys.KEY_ICON);

                                    String Constantfilter = cat_id.replaceAll("[^0-9,]","");
                                    Log.i("CONSTANT :",Constantfilter);

                                   String[] numbers = Constantfilter.split(",");
                                    for (int i=0;i<numbers.length;i++) {
                                        Log.i("Number: ", numbers[i]);

                                      if(numbers[i].equals(Keys.CONSTANT_CAT)) {
                                            if (innerObject.getString(Keys.KEY_VIEWTYPE).equals("exit") || innerObject.getString(Keys.KEY_VIEWTYPE).equals("grid,exit")) {
                                                model.setName(name);
                                                model.setView_type(viewtype);
                                                model.setId(id);
                                                model.setImage(KEY_ICONURL + image);
                                                model.setCat_id(cat_id);
                                                model.setGrid_order_id(grid_order_id);
                                                list.add(model);
                                            }

                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                dialog.dismiss();

            if (list.size() > 0) {
                try {
                    adapter.notifyDataSetChanged();
                    dataPath = objectToFile(list);

                    editor.putString("key_name5",dataPath);  // Saving string

                    // Save the changes in SharedPreferences
                    editor.commit(); // commit changes

                   // pref.getString("key_name5", null);
                    //   list = (ArrayList<MyDataModel>)objectFromFile(dataPath);
                    Snackbar.make(findViewById(R.id.parentLayout),  pref.getString("key_name5", null), Snackbar.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }

【问题讨论】:

  • 将json保存在共享首选项中并再次使用
  • SharedPreferences 如果您的数据很小,SQLite 如果您的数据很大。
  • 怎么样?请给出我使用但无法正常工作的任何示例 SharedPreferences
  • 使用 sqlite 数据库

标签: android json


【解决方案1】:

如果您的数据是结构化的并且您希望部分访问它,您最好使用SQLite

如果您只是想缓存这些数据,并且将来会完全使用它,最好使用SharedPref,因为您不会为它创建架构,因此 SQLite 将浪费资源和时间。

【讨论】:

  • 检查数据是否存在 sharedPref if(sharedPref.contains(KEY) ) // 从 sharedPref 加载数据 else // 进行网络调用
【解决方案2】:

您好,您可以使用任何名称创建一个类,例如我创建了“LocalSharedPrefrences”

 public class LocalSharedPrefrences {
    private static final String strSharedPrefName = "AppPrefrence";
    private static final String homePageJosn = "homePageJosn";
public static void saveHomePageJosnData(Context context, String JsonData) {
        SharedPreferences pref = context.getSharedPreferences(strSharedPrefName, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(homePageJosn, JsonData);
        editor.commit();
    }

    public static String getHomePageJosnData(Context context) {
        SharedPreferences pref = context.getSharedPreferences(strSharedPrefName, context.MODE_PRIVATE);
        String JsonData = pref.getString(homePageJosn, "");
        return JsonData;
    }
}

然后你可以使用这些函数来保存和获取json数据,如下所示:

LocalSharedPrefrences.saveHomePageJosnData(Context,JsonDataToSave);

String getJsonData = LocalSharedPrefrences.getHomePageJosnData(Context)

【讨论】:

  • JsonDataToSave 在哪个参数传递中?
  • 您需要在 saveHomePageJosnData 函数的第二个位置传递要保存在共享优先级中的字符串 jsonData。
【解决方案3】:

是的,您可以保存您的 json 数据,并且可以通过在 android 中使用 SharedPreferences 离线使用它。

首先你需要java类,即使用Json的pojo类,将该json转换为上面的类对象。

保存那个 json -->

private SharedPreferences preferences;
preferences = context.getSharedPreferences("Name", Context.MODE_PRIVATE);
editor = preferences.edit();
editor.putString("your Key String", gson.toJson(here will be your json class object));
editor.commit();

检索那个 Json-->

gson.fromJson(preferences.getString("your same Key String", null), your json class.class);

在此您将获得该 pojo 类的对象,即转换后的 json 类对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多