【问题标题】:I can re-call using retrofit 2我可以使用改造 2 重新调用
【发布时间】:2018-10-08 13:10:06
【问题描述】:

onFailure 手机没网时可以重新通话吗?

代码:

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
        }
    });
}

如果我不能重新调用,还有其他方法吗?

【问题讨论】:

标签: android android-studio retrofit retrofit2


【解决方案1】:

根据documentation

使用clone() 使用相同的参数进行多次调用以 相同的网络服务器;这可用于实现轮询或 重试失败的调用

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...
                    }
                });
            }
        }
    });

为了帮助跟踪连接:

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = Objects.requireNonNull(connectivityManager).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

【讨论】:

  • 恕我直言,答案不好。代码看起来超级冗长,如果我想多次重试,我不清楚该怎么做,例如直到我得到回应。
猜你喜欢
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 2016-10-20
  • 2018-05-15
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
相关资源
最近更新 更多