I am fetching data from API using Volley and showing in recycler view and showing progress bar till data isn't loaded, now the problem is sometime it takes a lot of time to fetch data and it keeps showing progress bar too . What I want is, if it takes more than 10 sec then progress bar should stop and show check internet connection with retry button on another layout.
Here is my code.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab_one, container, false);
SharedPreferences sharedPref = getActivity().getSharedPreferences("country", Context.MODE_PRIVATE);
String country = sharedPref.getString("selectedCountry", "au");
progressBar = (ProgressBar) view.findViewById(R.id.pbFragOne);
listRecyclerView = (RecyclerView) view.findViewById(R.id.headlines_recycler);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
listRecyclerView.setLayoutManager(linearLayoutManager);
listRecyclerView.setItemAnimator(new DefaultItemAnimator());
listRecyclerView.setHasFixedSize(true);
latestNewsResponse();
return view;
}
public void latestNewsResponse() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String str_response = response.toString();
Gson gson = new Gson();
News news = gson.fromJson(str_response, News.class);
news_list = news.getArticles();
if (!news.getStatus().contentEquals("ok")) {
Toast.makeText(getContext(), "Check your connection", Toast.LENGTH_SHORT).show();
}
newsListAdapter = new NewsListAdapter(getContext(), news_list);
progressBar.setVisibility(View.GONE);
listRecyclerView.setAdapter(newsListAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
}