我正在使用齐射从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);
}
}
答案 0 :(得分:1)
您必须这样做
1-通过内部数据库中的数据显示列表视图(如果有)
2-从服务器获取新数据
3-清除内部数据库中的先前数据(如果成功完成)
4-在内部数据库中保存新数据
5-使用列表视图向用户显示提取的数据