gpt4 book ai didi

java - 将 JSON 存储到 SharedpReferences

转载 作者:行者123 更新时间:2023-12-01 13:10:22 25 4
gpt4 key购买 nike

我正在 Android 中实现 JSON 解析。应用程序连接到 url 以检索 JSON 并返回字符串,一切正常。现在我想将 JSON 字符串存储到 ShareReferences,以便用户在没有互联网连接的情况下仍然可以看到 ListView。这是我的代码

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();
}

如果我有互联网连接,该应用程序运行正常。然后我关闭互联网,再次运行应用程序并收到此错误,即使我收到了 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)

我在这里做错了什么?谢谢

最佳答案

错误来自这一行:

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

我敢打赌,当未检测到连接时,listDataHeader 为 null,这是因为您只是在该 block 中初始化该对象:

if (isInternetPresent) {
...
}

只需在此 block 外部(上方)对其进行初始化,即可使其即使在没有连接时也可访问。

关于java - 将 JSON 存储到 SharedpReferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22918872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com