我可以使用改造2进行呼叫

时间:2018-10-05 21:45:08

标签: android android-studio retrofit retrofit2

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

如果我无法拨打电话,还有另一种方法吗?

1 个答案:

答案 0 :(得分:0)

根据documentation

  

使用clone()进行具有相同参数的多个调用以   同一台Web服务器;这可用于实施轮询或   重试失败的通话

Console.In

为帮助跟踪连接性:

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