How should I handle “No internet connection” with Retrofit on Android

前端 未结 8 929
一个人的身影
一个人的身影 2020-12-02 03:55

I\'d like to handle situations when there is no internet connection. Usually I\'d run:

ConnectivityManager cm =
    (ConnectivityManager)context.getSystemSer         


        
8条回答
  •  囚心锁ツ
    2020-12-02 04:52

    just do this you will notified even for issues like

    UnknownHostException

    ,

    SocketTimeoutException

    and others.

     @Override public void onFailure(Call> call, Throwable t) {  
    if (t instanceof IOException) {
        Toast.makeText(ErrorHandlingActivity.this, "this is an actual network failure :( inform the user and possibly retry", Toast.LENGTH_SHORT).show();
        // logging probably not necessary
    }
    else {
        Toast.makeText(ErrorHandlingActivity.this, "conversion issue! big problems :(", Toast.LENGTH_SHORT).show();
        // todo log to some central bug tracking service
    } }
    

提交回复
热议问题