【发布时间】:2019-01-19 07:59:09
【问题描述】:
我正在使用 volley 从 json 中获取数据,并且我想将数据添加到缓存中。我添加到缓存中,它在离线模式下工作正常,但是当它处于在线模式时,由于从 json api 获取数据,每个数据都会翻倍。所以我的问题是如何向用户显示我的缓存数据,直到获取 json 数据然后显示新数据并使用新数据更新缓存。
这是我读取缓存数据和获取在线数据的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_tab1, container, false);
list1=(ListView)v.findViewById(R.id.kunlist);
list2=(ListView)v.findViewById(R.id.oylist);
list3=(ListView)v.findViewById(R.id.haflist);
listitem=new ArrayList<>();
itemList2=new ArrayList<>();
itemList3=new ArrayList<>();
loadfirst();
loadingView();
// Inflate the layout for this fragment
return v;
}
private void loadingView() {
StringRequest request=new StringRequest(Request.Method.GET, URL_Data,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
JSONObject array=jsonObject.getJSONObject("data");
ObjectOutput out=new ObjectOutputStream(new FileOutputStream(new File(activity2.getCacheDir(),"")+File.separator+"cache.srl"));
out.writeObject(array.toString());
out.close();
jsonRead(array);
} catch (JSONException e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Activity activity = getActivity();
if(activity != null && isAdded()&&getContext()!=null)
Toast.makeText(getContext(),"Internet problem",Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(context);
requestQueue.add(request);
}
public void loadfirst(){
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(activity2.getCacheDir()+File.separator+"cache.srl")));
JSONObject jObject=new JSONObject((String)in.readObject());
in.close();
jsonRead(jObject);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void jsonRead(JSONObject obj) throws JSONException, UnsupportedEncodingException {
JSONArray day=obj.getJSONArray("day");
JSONArray week=obj.getJSONArray("week");
JSONArray month=obj.getJSONArray("month");
KunItem item=null;
KunItem kun=null;
KunItem hafta=null;
for(int i=0;i<day.length();i++) {
JSONObject o = day.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
item = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
} else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
item = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code"));
}
listitem.add(item);
adapter = new KunAdapter(context, listitem);
list1.setAdapter(adapter);
}
for(int i=0;i<week.length();i++) {
JSONObject o = week.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
kun = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
}
else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
kun = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code"));
}
itemList2.add(kun);
adapter2 = new KunAdapter(context, itemList2);
list3.setAdapter(adapter2);
}
for(int i=0;i<month.length();i++) {
JSONObject o = month.getJSONObject(i);
if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("uz")) {
hafta = new KunItem(
o.getString("name_uz"),
o.getString("price"),
o.getString("activation_code")
);
} else if (Tab1.this.res.getConfiguration().locale.getLanguage().equals("ru")) {
hafta = new KunItem(
URLDecoder.decode(URLEncoder.encode(o.getString("name_ru"), "iso8859-1"), "UTF-8"),
o.getString("price"),
o.getString("activation_code")
);
}
itemList3.add(hafta);
adapter3=new KunAdapter(context,itemList3);
list2.setAdapter(adapter3);
}
}
【问题讨论】:
标签: android json listview caching