Minh Phan
Minh Phan

Reputation: 47

storing JSON to SharedReferences

I'm implementing JSON Parse in Android. The app connect to the url to retrieve JSON and return a String, everything work fine. Now I want to store JSON String to ShareReferences so user still can see the ListView if no internet connection. Here is my code

if (isInternetPresent) { //check internet connection
                ServiceHandler sh = new ServiceHandler();
                listDataHeader = new ArrayList<String>();
                listDataChild = new HashMap<String, List<TVSchedModel>>();
                // Making a request to url and getting response
                jsonStr = sh.makeServiceCall(URL, ServiceHandler.GET); //connect to url and return a string
                Editor editor = sharedpreferences.edit();
                editor.putString("JSON", jsonStr);
                editor.commit();
            } else {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getActivity(),
                                "Cannot connect to server", Toast.LENGTH_SHORT)
                                .show();
                    }
                });
                jsonStr = sharedpreferences.getString("JSON", "");
                Log.i("JSON Shared", jsonStr);
            }
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                JSONArray chan = jsonObj.getJSONArray(TAG_CHANNEL);
                for (int i = 0; i < chan.length(); i++) {
                    listDataHeader.add(chan.getJSONObject(i).getString(
                            "Channel"));
                    JSONArray sched = jsonObj.getJSONArray(chan
                            .getJSONObject(i).getString("Channel"));
                    List<TVSchedModel> result = new ArrayList<TVSchedModel>();
                    for (int k = 0; k < sched.length(); k++) {
                        result.add(convertSchedList(sched.getJSONObject(k)));
                    }
                    listDataChild.put(listDataHeader.get(i), result);
                }

                listAdapter.setHeaderList(listDataHeader);
                return listDataChild;
            } catch (Throwable t) {
                t.printStackTrace();
            }

If I have internet connection, the app runs fine. Then I turn off the internet, run the app again and get this error even I got the string back that show at Log.i(...)

04-07 23:59:30.066: W/System.err(22797): java.lang.NullPointerException
04-07 23:59:30.066: W/System.err(22797):    at com.pnminh.dfytask.TVSchedFragment$AsyncListViewLoader.doInBackground(TVSchedFragment.java:187)
04-07 23:59:30.066: W/System.err(22797):    at com.pnminh.dfytask.TVSchedFragment$AsyncListViewLoader.doInBackground(TVSchedFragment.java:1)
04-07 23:59:30.066: W/System.err(22797):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-07 23:59:30.066: W/System.err(22797):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-07 23:59:30.066: W/System.err(22797):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-07 23:59:30.066: W/System.err(22797):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-07 23:59:30.066: W/System.err(22797):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-07 23:59:30.066: W/System.err(22797):    at java.lang.Thread.run(Thread.java:841)

What did I do wrong here? Thanks

Upvotes: 0

Views: 42

Answers (1)

nKn
nKn

Reputation: 13761

The error is coming from this line:

listDataHeader.add(chan.getJSONObject(i).getString("Channel"));

My bet is that listDataHeader is null when no connectivity is detected, and this is because you're just initializing this object in this block:

if (isInternetPresent) {
  ...
}

Simply initialize it outside (above) this block to let it be reachable even when there's no connectivity.

Upvotes: 1

Related Questions