Abhinav Srivastava
Abhinav Srivastava

Reputation: 185

Retrofit giving 'No Internet' error even when phone is connected to internet

I am using retrofit library to download JSON and process it with GSON to show results in recyclerview. However some of my users are complaining that they get 'no internet' error even when they are connected online. Can some one point out what I am missing in my code. Thanks in advance.

Here is my api code.

public class BhartiAPI {
public static final String url = "https://www.ddbharat.com/api/";

public static PostService postService = null;

public static PostService getService() {

    if(postService == null) {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60 / 2, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .cache(null)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        postService = retrofit.create(PostService.class);
    }
    return postService;
}

public interface PostService {
    @Headers("Cache-Control: no-cache")
    @POST
    Call<PostList> getPostList(@Url String url);
}

And this is how I am calling it.

final String url = BhartiAPI.url + fileName;
final Call<PostList> postList = BhartiAPI.getService().getPostList(url);

postList.enqueue(new Callback<PostList>() {
        @SuppressLint("ResourceType")
        @Override
        public void onResponse(Call<PostList> call, Response<PostList> response) {

            if (response.isSuccessful()) {
                noNet.setVisibility(View.GONE);
                list = response.body();
                items.addAll(list.getItems());
                adapter.notifyDataSetChanged();
                progressBar.setVisibility(View.GONE);

            } else {
                // error case
                switch (response.code()) {
                    case 404:
                        getData2("news");
                        break;
                    case 500:
                        getData2("news");
                        break;
                    default:
                        getData2("news");
                        break;
                }
            }
        }

        @Override
        public void onFailure(Call<PostList> call, Throwable t) {

            if (t instanceof UnknownHostException) {
                noNet.setVisibility(View.VISIBLE);
            } else if (t instanceof IOException) {
                noNet.setVisibility(View.GONE);
            } else {
                noNet.setVisibility(View.GONE);
            }
            getData2("news");

        }
    });

Here sometimes this 'if (t instanceof UnknownHostException)' is being called again and again even when internet is connected and working fine.

Upvotes: 0

Views: 352

Answers (1)

Izzy Stannett
Izzy Stannett

Reputation: 252

I often get this error when I change internet connections on my device, and run the same app on more than one connection. The solution for me is to run "Invalidate Caches and Restart" in Android Studio.

Upvotes: 1

Related Questions