2

Can I re-call when there is no internet in mobile in onFailure?

Code:

private void ShowData() {
    rv_categories.setVisibility(View.GONE);
    txt_loading.setVisibility(View.VISIBLE);
    Loading();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("*****").addConverterFactory(GsonConverterFactory.create()).build();
    RetrofitService retrofitService = retrofit.create(RetrofitService.class);
    Call<List<CategoriesModels>> call = retrofitService.get_categories();
    call.enqueue(new Callback<List<CategoriesModels>>() {
        @Override
        public void onResponse(Call<List<CategoriesModels>> call, Response<List<CategoriesModels>> response) {
            List<CategoriesModels> list = response.body();
            for (CategoriesModels i : list) {
                categoriesModels.add(new CategoriesModels(i.getId(), i.getTitle(), i.getPhoto(), i.getShortcut()));
            }
            Collections.shuffle(categoriesModels);
            categoriesAdapter = new CategoriesAdapter(CategoriesActivity.this, categoriesModels);
            rv_categories.setLayoutManager(new LinearLayoutManager(CategoriesActivity.this, LinearLayoutManager.VERTICAL, false));
            rv_categories.setAdapter(categoriesAdapter);
            rv_categories.setVisibility(View.VISIBLE);
            txt_loading.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<List<CategoriesModels>> call, Throwable t) {
            //I want when there is no Internet try call again
        }
    });
}

If I can't re-call, is there another way?

dferenc
  • 7,918
  • 12
  • 41
  • 49
Taha Sami
  • 1,565
  • 1
  • 16
  • 43

1 Answers1

0

According to the documentation:

Use clone() to make multiple calls with the same parameters to the same web server; this may be used to implement polling or to retry a failed call

private void ShowData() {

    // ...

    Call<List<CategoriesModels>> call = retrofitService.get_categories();
    call.enqueue(new Callback<List<CategoriesModels>>() {
        @Override
        public void onResponse(@NonNull Call<List<CategoriesModels>> call,
                               @NonNull Response<List<CategoriesModels>> response) {
            // ...
        }

        @Override
        public void onFailure(@NonNull Call<List<CategoriesModels>> call, @NonNull Throwable t) {
            // I want when there is no Internet try call again

            // if not connected to network, create a new, identical call & re-try.
            if (!isNetworkAvailable()) {
                call.clone().enqueue(new Callback<List<CategoriesModels>>() {
                    @Override
                    public void onResponse(@NonNull Call<List<CategoriesModels>> call,
                                           @NonNull Response<List<CategoriesModels>> response) {
                        // re-try successful - handle response...
                    }

                    @Override
                    public void onFailure(@NonNull Call<List<CategoriesModels>> call,
                                          @NonNull Throwable t) {
                        // re-try failed - handle failure...
                    }
                });
            }
        }
    });

To help keep track of connectivity:

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = Objects.requireNonNull(connectivityManager).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
chornge
  • 2,021
  • 3
  • 28
  • 37
  • IMHO bad answer. The code looks super verbose and it is unclear what to do if I want to retry more than one time, e.g. until I get response. – Oleg Yablokov Aug 08 '20 at 10:00