新的 json 数据未在 android 中显示
Posted
技术标签:
【中文标题】新的 json 数据未在 android 中显示【英文标题】:new json data is not showing in android 【发布时间】:2021-12-12 14:47:41 【问题描述】:从我的服务器检索 json 数据。第一次在我的应用程序中显示数据。但是在更新新数据时,它没有显示。只显示旧数据..当我从应用程序中清除缓存时,它会显示更新的数据。但我想同时显示实时数据。我必须做什么?请帮帮我。
Home.java
public class Home extends Fragment
private final String JSON_URL = "https://mywebsite.org/news/api.php";
private JsonArrayRequest request ;
private RequestQueue requestQueue ;
private List<Anime> lstAnime ;
private RecyclerView recyclerView ;
ProgressBar loading;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View root = inflater.inflate(R.layout.fragment_home, container, false);
loading = (ProgressBar)root.findViewById(R.id.bar);
loading.setMax(100);
lstAnime = new ArrayList<>() ;
recyclerView = root.findViewById(R.id.recyclerviewid);
jsonrequest();
return root;
private void jsonrequest()
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
loading.setVisibility(View.GONE);
JSONObject jsonObject = null ;
for (int i = 0 ; i < response.length(); i++ )
try
jsonObject = response.getJSONObject(i) ;
Anime anime = new Anime() ;
anime.setName(jsonObject.getString("title"));
anime.setDescription(jsonObject.getString("description"));
anime.setDate(jsonObject.getString("date"));
anime.setCategorie(jsonObject.getString("category"));
anime.setAuthor(jsonObject.getString("admin"));
anime.setImage_url(jsonObject.getString("thumbnail"));
lstAnime.add(anime);
catch (JSONException e)
e.printStackTrace();
setuprecyclerview(lstAnime);
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
if (error instanceof TimeoutError || error instanceof NoConnectionError)
//This indicates that the request has either time out or there is no connection
Toast.makeText(getActivity(), "TimeOut...! No Internet",
Toast.LENGTH_LONG).show();
else if (error instanceof AuthFailureError)
// Error indicating that there was an Authentication Failure while performing the request
Toast.makeText(getActivity(), "Failed To Receive Data",
Toast.LENGTH_LONG).show();
else if (error instanceof ServerError)
//Indicates that the server responded with a error response
Toast.makeText(getActivity(), "Our Server Under Maintenance",
Toast.LENGTH_LONG).show();
else if (error instanceof NetworkError)
//Indicates that there was network error while performing the request
Toast.makeText(getActivity(), "Network Not Responding",
Toast.LENGTH_LONG).show();
else if (error instanceof ParseError)
// Indicates that the server response could not be parsed
Toast.makeText(getActivity(), "Please Reload Again!",
Toast.LENGTH_LONG).show();
);
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
private void setuprecyclerview(List<Anime> lstAnime)
RecyclerViewAdapter myadapter = new RecyclerViewAdapter(getActivity(),lstAnime);
recyclerView.setAdapter(myadapter);
// grid columns is 2
GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
@Override
public int getSpanSize(int position)
// 0 is first one and 9 is first one all of others are 2 columns
if (position == 0 || position == 9)
return 2;
return 1;
);
recyclerView.setItemAnimator(null);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(myadapter);
recyclerView.setLayoutManager(manager);
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
对不起,我的英语不好。 提前致谢。
【问题讨论】:
【参考方案1】:我没有看到您初始化 xml 文件中包含的 SwipeRefreshLayout
的地方。您的片段应该实现SwipeRefreshLayout.OnRefreshListener
并覆盖onRefresh()
方法以触发您的网络请求。当使用swipeRefreshLayout.setRefreshing(boolean)
完成网络请求时,不要忘记关闭 SwipeRefreshLayout 加载器
您可以查看文档了解更多详情:https://developer.android.com/training/swipe
这是您实现 SwipeRefreshLayout 的片段。
public class Home extends Fragment implements SwipeRefreshLayout.OnRefreshListener
private final String JSON_URL = "https://mywebsite.org/news/api.php";
private JsonArrayRequest request ;
private RequestQueue requestQueue ;
private List<Anime> lstAnime ;
private RecyclerView recyclerView ;
ProgressBar loading;
private SwipeRefreshLayout swipeRefreshLayout; //declare SwipeRefreshLayout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View root = inflater.inflate(R.layout.fragment_home, container, false);
swipeRefreshLayout = (SwipeRefreshLayout)root.findViewById(R.id.swipeContainer);
swipeRefreshLayout.setOnRefreshListener(this); //add refresh listener
loading = (ProgressBar)root.findViewById(R.id.bar);
loading.setMax(100);
lstAnime = new ArrayList<>() ;
recyclerView = root.findViewById(R.id.recyclerviewid);
jsonrequest();
return root;
private void jsonrequest()
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
swipeRefreshLayout.setRefreshing(false); //Dismiss SwipeRefreshLayout loader
loading.setVisibility(View.GONE);
JSONObject jsonObject = null ;
for (int i = 0 ; i < response.length(); i++ )
try
jsonObject = response.getJSONObject(i) ;
Anime anime = new Anime() ;
anime.setName(jsonObject.getString("title"));
anime.setDescription(jsonObject.getString("description"));
anime.setDate(jsonObject.getString("date"));
anime.setCategorie(jsonObject.getString("category"));
anime.setAuthor(jsonObject.getString("admin"));
anime.setImage_url(jsonObject.getString("thumbnail"));
lstAnime.add(anime);
catch (JSONException e)
e.printStackTrace();
setuprecyclerview(lstAnime);
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
swipeRefreshLayout.setRefreshing(false); //Dismiss SwipeRefreshLayout loader
if (error instanceof TimeoutError || error instanceof NoConnectionError)
//This indicates that the request has either time out or there is no connection
Toast.makeText(getActivity(), "TimeOut...! No Internet",
Toast.LENGTH_LONG).show();
else if (error instanceof AuthFailureError)
// Error indicating that there was an Authentication Failure while performing the request
Toast.makeText(getActivity(), "Failed To Receive Data",
Toast.LENGTH_LONG).show();
else if (error instanceof ServerError)
//Indicates that the server responded with a error response
Toast.makeText(getActivity(), "Our Server Under Maintenance",
Toast.LENGTH_LONG).show();
else if (error instanceof NetworkError)
//Indicates that there was network error while performing the request
Toast.makeText(getActivity(), "Network Not Responding",
Toast.LENGTH_LONG).show();
else if (error instanceof ParseError)
// Indicates that the server response could not be parsed
Toast.makeText(getActivity(), "Please Reload Again!",
Toast.LENGTH_LONG).show();
);
requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
private void setuprecyclerview(List<Anime> lstAnime)
RecyclerViewAdapter myadapter = new RecyclerViewAdapter(getActivity(),lstAnime);
recyclerView.setAdapter(myadapter);
// grid columns is 2
GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
@Override
public int getSpanSize(int position)
// 0 is first one and 9 is first one all of others are 2 columns
if (position == 0 || position == 9)
return 2;
return 1;
);
recyclerView.setItemAnimator(null);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(myadapter);
recyclerView.setLayoutManager(manager);
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
@Override
public void onRefresh()
//Whatever is here will be executed when the user swipe to refresh
jsonrequest();
【讨论】:
它不工作.... 尝试两三次后,应用程序自动停止...并且也没有更新新数据。 @Litu 拉动刷新时会触发jsonrequest
方法吗?
当我拉动刷新时,它只是眨了眨眼,让应用程序太慢......尝试一两次时,应用程序停止了。并且没有新的数据更新。【参考方案2】:
如果您想在服务器上的数据更新后立即更新应用程序中的数据,那么您应该使用 Web-Sockets 而不是 API,因为 API 只有在您使用正确的凭据点击它时才会给出响应。
【讨论】:
没有网络套接字我可以实时更新我的数据吗? api 已经从我的数据库中响应。以上是关于新的 json 数据未在 android 中显示的主要内容,如果未能解决你的问题,请参考以下文章
Mongoose .populate 未在 JSON 中显示