在我的应用程序中,我想检查互联网并在连接时加载一些数据,如果没有连接检查数据 5000ms 并检查此3级(15000ms),如果在此15000ms未连接显示用户&# 34;互联网未连接"当连接互联网获取此数据时。
我的代码:
private void retrofitData(boolean isConnect) {
if (isConnect) {
try {
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<Retrofit_ColoniesModelResponse> call = apiInterface.getColoniesResponse();
call.enqueue(new Callback<Retrofit_ColoniesModelResponse>() {
@Override
public void onResponse(Call<Retrofit_ColoniesModelResponse> call, Response<Retrofit_ColoniesModelResponse> response) {
try {
if (response != null) {
if (response.body() != null) {
// first remove
models.clear();
// then fill
models.addAll(response.body().getCategories());
mAdaper.notifyDataSetChanged();
colonies_RecyclerView.setAdapter(mAdaper);
CustomProcessDialog.dissmis();
showHelp();
} else {
CustomProcessDialog.dissmis();
new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
.setTitleText("title")
.setContentText("message")
.setConfirmText("ok")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
finish();
startActivity(getIntent());
}
})
.show();
}
} else {
CustomProcessDialog.createAndShow(context);
TastyToast.makeText(context, "error", TastyToast.LENGTH_LONG, TastyToast.ERROR);
}
checkNetLayout.setVisibility(View.GONE);
} catch (Exception e) {
}
}
@Override
public void onFailure(Call<Retrofit_ColoniesModelResponse> call, Throwable t) {
new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
.setTitleText("title")
.setContentText("message")
.setConfirmText("ok")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
finish();
startActivity(getIntent());
}
})
.show();
}
});
} catch (Exception e) {
Log.e("ServerEx", "Error : " + e);
}
} else {
checkNetLayout.setVisibility(View.VISIBLE);
if (mAdaper != null) {
mAdaper.clear();
colonies_RecyclerView.setAdapter(mAdaper);
}
categoryCheckNet_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadData();
}
});
}
}
我怎样才能做到这一点?请帮我。我很业余。
答案 0 :(得分:2)
将此方法放在您设置实用程序方法的Conastant类中:
io.circe.Json
并在主要活动中使用它,如:
public abstract static class BackoffCallback<T> implements Callback<T> {
private static int RETRY_COUNT = 3;
private static final double RETRY_DELAY = 5000;
private int retryCount = 0;
protected BackoffCallback(int retryNum) {
RETRY_COUNT=retryNum;
}
@Override
public void onFailure(final Call<T> call, Throwable t) {
retryCount++;
if (retryCount <= RETRY_COUNT) {
int expDelay = (int) (RETRY_DELAY);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
retry(call);
}
}, expDelay);
} else {
onFailedAfterRetry(t);
}
}
private void retry(Call<T> call) {
call.clone().enqueue(this);
}
public abstract void onFailedAfterRetry(Throwable t);
}