Show progress bar while reconnecting to internet

Show progress bar while reconnecting to internet



When there is no internet, an alert dialog appear with cancel and retry button, and on retry button I am recursively calling newsResponse() method.



Now what I want is, on retry it should show progress dialog (means 5 seconds delay) and when there is no internet then again show alert dialog.



My Code.


public void newsResponse()
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
@Override
public void onResponse(JSONObject response)
String str_response = response.toString();
News news = gson.fromJson(str_response, News.class);
news_list = news.getArticles();
newsListAdapter = new NewsListAdapter(NewsActivity.this, news_list);
newsGridAdapter = new NewsGridAdapter(NewsActivity.this, news_list);
progressBar.setVisibility(View.GONE);
listRecyclerView.setAdapter(newsListAdapter);
gridRecyclerView.setAdapter(newsGridAdapter);
if (isList)
listRecyclerView.setVisibility(View.GONE);
else
gridRecyclerView.setVisibility(View.GONE);


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
if (!checkInternet())
AlertDialog.Builder connectionBuilder = new AlertDialog.Builder(NewsActivity.this);
connectionBuilder.setMessage("Unable to load data!");
connectionBuilder.setCancelable(true);
connectionBuilder.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
newsResponse();

);

connectionBuilder.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
finish();

);

AlertDialog alert11 = connectionBuilder.create();
alert11.show();


);


private boolean checkInternet()
ConnectivityManager cm =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

return isConnected;




1 Answer
1



Add delay to your progess bar:


public void newsResponse()
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
@Override
public void onResponse(JSONObject response)
String str_response = response.toString();
News news = gson.fromJson(str_response, News.class);
news_list = news.getArticles();
newsListAdapter = new NewsListAdapter(NewsActivity.this, news_list);
newsGridAdapter = new NewsGridAdapter(NewsActivity.this, news_list);
progressBar.setVisibility(View.GONE);
listRecyclerView.setAdapter(newsListAdapter);
gridRecyclerView.setAdapter(newsGridAdapter);
if (isList)
listRecyclerView.setVisibility(View.GONE);
else
gridRecyclerView.setVisibility(View.GONE);


, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
if (!checkInternet())
AlertDialog.Builder connectionBuilder = new AlertDialog.Builder(NewsActivity.this);
connectionBuilder.setMessage("Unable to load data!");
connectionBuilder.setCancelable(true);
connectionBuilder.setPositiveButton(
"Retry",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
final ProgressDialog pDialog = new ProgressDialog(NewsActivity.this);
pDialog.setMessage("Loading...");
pDialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable()
public void run()
pDialog.dismiss();
newsResponse();

, 5000);

);

connectionBuilder.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
dialog.cancel();
finish();

);

AlertDialog alert11 = connectionBuilder.create();
alert11.show();


);


private boolean checkInternet()
ConnectivityManager cm =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

return isConnected;



It will show progress dialog for 5 seconds when you press retry button.






sorry I didn't get it.

– Ubaid Tanoli
Sep 11 '18 at 5:41






edited, please check

– Sambit Mallick
Sep 11 '18 at 6:02






ok then while reconnecting internet?

– Sambit Mallick
Sep 11 '18 at 6:10






read my question again, I said when there is no internet my alert dialog appears and when I press retry, it should show connecting and if after 5 seconds internet is not active then again show my alert dialog.

– Ubaid Tanoli
Sep 11 '18 at 6:13






your header: Show progress bar while reconnecting to internet, your question: Now what I want is, on retry it should show progress bar (means 5 seconds delay). I answered the same adding delay for 5secs then you said not to show pregress bar while getting the data? Can you clarify or edit the question? Thank you! PS: seems like you deleted your comment

– Sambit Mallick
Sep 11 '18 at 6:15




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế