简体   繁体   English

如何在android中的retrofit 2.0中处理网络丢失

[英]How to handle network loss in retrofit 2.0 in android

I am using retrofit 2.0 for network request in my project. 我在我的项目中使用retrofit 2.0来处理网络请求。 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. 任何帮助将不胜感激。

@Override
    public void failure(RetrofitError arg0) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM