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

前端 未结 8 928
一个人的身影
一个人的身影 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 04:35

    you can use this code

    Response.java

    import com.google.gson.annotations.SerializedName;
    
    /**
     * Created by hackro on 19/01/17.
     */
    
    public class Response {
        @SerializedName("status")
        public String status;
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getStatus() {
            return status;
        }
    
        @SuppressWarnings({"unused", "used by Retrofit"})
        public Response() {
        }
    
        public Response(String status) {
            this.status = status;
        }
    }
    

    NetworkError.java

    import android.text.TextUtils;
    
    import com.google.gson.Gson;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    import retrofit2.adapter.rxjava.HttpException;
    
    import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
    
    /**
     * Created by hackro on 19/01/17.
     */
    
    public class NetworkError extends Throwable {
        public static final String DEFAULT_ERROR_MESSAGE = "Please try again.";
        public static final String NETWORK_ERROR_MESSAGE = "No Internet Connection!";
        private static final String ERROR_MESSAGE_HEADER = "Error Message";
        private final Throwable error;
    
        public NetworkError(Throwable e) {
            super(e);
            this.error = e;
        }
    
        public String getMessage() {
            return error.getMessage();
        }
    
        public boolean isAuthFailure() {
            return error instanceof HttpException &&
                    ((HttpException) error).code() == HTTP_UNAUTHORIZED;
        }
    
        public boolean isResponseNull() {
            return error instanceof HttpException && ((HttpException) error).response() == null;
        }
    
        public String getAppErrorMessage() {
            if (this.error instanceof IOException) return NETWORK_ERROR_MESSAGE;
            if (!(this.error instanceof HttpException)) return DEFAULT_ERROR_MESSAGE;
            retrofit2.Response response = ((HttpException) this.error).response();
            if (response != null) {
                String status = getJsonStringFromResponse(response);
                if (!TextUtils.isEmpty(status)) return status;
    
                Map> headers = response.headers().toMultimap();
                if (headers.containsKey(ERROR_MESSAGE_HEADER))
                    return headers.get(ERROR_MESSAGE_HEADER).get(0);
            }
    
            return DEFAULT_ERROR_MESSAGE;
        }
    
        protected String getJsonStringFromResponse(final retrofit2.Response response) {
            try {
                String jsonString = response.errorBody().string();
                Response errorResponse = new Gson().fromJson(jsonString, Response.class);
                return errorResponse.status;
            } catch (Exception e) {
                return null;
            }
        }
    
        public Throwable getError() {
            return error;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            NetworkError that = (NetworkError) o;
    
            return error != null ? error.equals(that.error) : that.error == null;
    
        }
    
        @Override
        public int hashCode() {
            return error != null ? error.hashCode() : 0;
        }
    }
    

    Implementation in your methods

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
    
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                networkError.setError(e);
                Log.e("Error:",networkError.getAppErrorMessage());
            }
    
            @Override
            public void onNext(Object obj) {   super.onNext(obj);        
        }
    

提交回复
热议问题