当没有互联网时,会出现一个警告对话框,其中显示取消和重试按钮,而在重试按钮上,我递归调用 newsResponse()方法。
现在我要的是,在重试上,它应该显示进度对话框(平均5秒延迟),并且当没有互联网时,再次显示警报对话框。>
我的代码。
Set
答案 0 :(得分:1)
在进度条上添加延迟:
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;
}
当您按下重试按钮时,它将显示进度对话框5秒钟。