diff --git a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java index f43aad35..a9395782 100644 --- a/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java +++ b/okgo/src/main/java/com/lzy/okgo/cache/policy/BaseCachePolicy.java @@ -16,6 +16,7 @@ package com.lzy.okgo.cache.policy; import android.graphics.Bitmap; +import android.text.TextUtils; import com.lzy.okgo.OkGo; import com.lzy.okgo.cache.CacheEntity; @@ -101,10 +102,11 @@ protected Response requestNetworkSync() { try { okhttp3.Response response = rawCall.execute(); int responseCode = response.code(); - //network error if (responseCode == 404 || responseCode >= 500) { - return Response.error(false, rawCall, response, HttpException.NET_ERROR()); + String message = response.message(); + String string = response.body().string(); + return Response.error(false, rawCall, response, new HttpException(responseCode, message, string)); } T body = request.getConverter().convertResponse(response); @@ -152,7 +154,9 @@ public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOEx //network error if (responseCode == 404 || responseCode >= 500) { - Response error = Response.error(false, call, response, HttpException.NET_ERROR()); + String message = response.message(); + String string = response.body().string(); + Response error = Response.error(false, call, response, new HttpException(responseCode, message, string)); onError(error); return; } diff --git a/okgo/src/main/java/com/lzy/okgo/exception/HttpException.java b/okgo/src/main/java/com/lzy/okgo/exception/HttpException.java index 6d3692f0..64cb91a2 100644 --- a/okgo/src/main/java/com/lzy/okgo/exception/HttpException.java +++ b/okgo/src/main/java/com/lzy/okgo/exception/HttpException.java @@ -34,10 +34,18 @@ public class HttpException extends RuntimeException { private String message; //HTTP status message private transient Response response; //The full HTTP response. This may be null if the exception was serialized + private String body; public HttpException(String message) { super(message); } + public HttpException(int code,String message,String body) { + super(message); + this.code = code; + this.message = message; + this.body = body; + } + public HttpException(Response response) { super(getMessage(response)); this.code = response.code(); @@ -45,6 +53,7 @@ public HttpException(Response response) { this.response = response; } + private static String getMessage(Response response) { HttpUtils.checkNotNull(response, "response == null"); return "HTTP " + response.code() + " " + response.message(); @@ -66,6 +75,19 @@ public static HttpException NET_ERROR() { return new HttpException("network error! http response code is 404 or 5xx!"); } + public int getCode() { + return code; + } + + @Override + public String getMessage() { + return message; + } + + public String getBody() { + return body; + } + public static HttpException COMMON(String message) { return new HttpException(message); }