Devesh Agrawal
Devesh Agrawal

Reputation: 9212

How to handle network loss in retrofit 2.0 in android

I am using retrofit 2.0 for network request in my project. Before making any request i am checking if user is connected with network then only proceed the request.

if(Connected()){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://example.com/cgi-bin/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService service = retrofit.create(APIService.class);



        Call<ArrayList<Brand>> call = service.loadBrand();
        call.enqueue(new Callback<ArrayList<Brand>>() {

            @Override
            public void onResponse(Response<ArrayList<Brand>> response , Retrofit retrofit)
            {

                brands = response.body();

                //brands.size();
                //JSONArray jsonArray_GR = response.body();
                //arrayCallback.onResponse(jsonArray_GR);
            }



            @Override
            public void onFailure(Throwable t) {

            }
        });
    }

The problem is how to handle the case if network connection lost in between while request still not completed.

What should i check and where?

Any help will be appreciated.

Upvotes: 1

Views: 605

Answers (1)

Kanaiya Katarmal
Kanaiya Katarmal

Reputation: 6108

@Override
    public void failure(RetrofitError arg0) {

        if (arg0.getCause() instanceof UnknownHostException) {
            //network loss in retrofit 2.0 in android
        }
    }

Upvotes: 3

Related Questions