/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.mvpart.demo.app;

import android.content.Context;
import android.net.ParseException;

import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import me.jessyan.art.utils.ArtUtils;
import me.jessyan.rxerrorhandler.handler.listener.ResponseErrorListener;
import retrofit2.HttpException;
import timber.log.Timber;

/**
 * ================================================
 * 展示 {@link ResponseErrorListener} 的用法
 * <p>
 * Created by JessYan on 04/09/2017 17:18
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class ResponseErrorListenerImpl implements ResponseErrorListener {

    @Override
    public void handleResponseError(Context context, Throwable t) {
        Timber.tag("Catch-Error").w(t.getMessage());
        //这里不光是只能打印错误,还可以根据不同的错误作出不同的逻辑处理
        String msg = "未知错误";
        if (t instanceof UnknownHostException) {
            msg = "网络不可用";
        } else if (t instanceof SocketTimeoutException) {
            msg = "请求网络超时";
        } else if (t instanceof HttpException) {
            HttpException httpException = (HttpException) t;
            msg = convertStatusCode(httpException);
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            msg = "数据解析错误";
        }
        ArtUtils.snackbarText(msg);
    }

    private String convertStatusCode(HttpException httpException) {
        String msg;
        if (httpException.code() == 500) {
            msg = "服务器发生错误";
        } else if (httpException.code() == 404) {
            msg = "请求地址不存在";
        } else if (httpException.code() == 403) {
            msg = "请求被服务器拒绝";
        } else if (httpException.code() == 307) {
            msg = "请求被重定向到其他页面";
        } else {
            msg = httpException.message();
        }
        return msg;
    }

}

@@ -1605,16 +1605,16 @@
 %E8%BF%99%E9%87%8C%E4%B8%8D%E5%85%89
-%E6%98%AF
 %E5%8F%AA%E8%83%BD%E6%89%93%E5%8D%B0%E9%94%99%E8%AF%AF,
+ 
 %E8%BF%98%E5%8F%AF%E4%BB%A5%E6%A0%B9
@@ -1623,9 +1623,9 @@
 %E5%90%8C%E7%9A%84%E9%94%99%E8%AF%AF
-%E4%BD%9C
+%E5%81%9A
 %E5%87%BA%E4%B8%8D%E5%90%8C%E7%9A%84
@@ -1629,16 +1629,80 @@
 %E4%B8%8D%E5%90%8C%E7%9A%84%E9%80%BB%E8%BE%91%E5%A4%84%E7%90%86%0A
+        //%E8%BF%99%E9%87%8C%E5%8F%AA%E6%98%AF%E5%AF%B9%E5%87%A0%E4%B8%AA%E5%B8%B8%E7%94%A8%E9%94%99%E8%AF%AF%E8%BF%9B%E8%A1%8C%E7%AE%80%E5%8D%95%E7%9A%84%E5%A4%84%E7%90%86, %E5%B1%95%E7%A4%BA%E8%BF%99%E4%B8%AA%E7%B1%BB%E7%9A%84%E7%94%A8%E6%B3%95, %E5%9C%A8%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E4%B8%AD%E8%AF%B7%E6%82%A8%E8%87%AA%E8%A1%8C%E5%AF%B9%E6%9B%B4%E5%A4%9A%E9%94%99%E8%AF%AF%E8%BF%9B%E8%A1%8C%E6%9B%B4%E4%B8%A5%E8%B0%A8%E7%9A%84%E5%A4%84%E7%90%86%0A
         
@@ -2772,11 +2772,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Improve ResponseErrorListenerImpl's comments