Java 当没有internet连接(数据来自服务器)时,如何存储列表项
关于Java 当没有internet连接(数据来自服务器)时,如何存储列表项,java,android,json,listview,android-volley,Java,Android,Json,Listview,Android Volley,关于 我有一个列表视图包含4项,其中3项是文本,第四项是图像 所有数据都在json中的服务器上 代码正常工作,但当internet关闭时,所有项目和列表均未显示,如何使列表在有或无internet连接的情况下工作 因为我每天都向数据库添加新项目 这是我的密码 requestQueue = Volley.newRequestQueue(this); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Reques
我有一个列表视图包含4项,其中3项是文本,第四项是图像 所有数据都在json中的服务器上 代码正常工作,但当internet关闭时,所有项目和列表均未显示,如何使列表在有或无internet连接的情况下工作 因为我每天都向数据库添加新项目 这是我的密码
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("allstudents");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
String id = respons.getString("id");
String name = respons.getString("name");
String info = respons.getString("info");
String img = respons.getString("img");
link = respons.getString("link");
voicelink = respons.getString("voicelink");
listitmes.add(new listitme(id, name, info, img, link, voicelink));
allitems();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
public void allitems() {
listAdapter lsadapter = new listAdapter(listitmes);
listView.setAdapter(lsadapter);
}
requestQueue=Volley.newRequestQueue(此);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“所有学生”);
for(int i=0;i
我搜索了很多,但没有找到答案
我在StackOverflow.Com上找不到类似的东西
使用共享的SharedReferences,您可以保存列表,如果没有wifi,可以调用它 您可以做的是:
- 从Internet接收数据后,立即将下载的数据保存到文件()/properties()中。()
- 获取数据时活动中的消息(例如上次同步:时间戳)
- 如果无法连接到Internet以获取新数据,请从文件/属性加载数据
- 如果您无法连接到Internet并且没有任何保存的数据,请显示一条消息(例如,无法连接到Internet-可能将其着色以突出显示这是一个错误)
我的2美分…我知道没有互联网我无法连接,但我想知道,即使没有连接,我也无法存储。我懂了。看看SharedReferences。您可以脱机保存数据,然后将其回调。这正是我想要的,但不幸的是,没有教程能做到这一点