Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1 extra #889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,10 +102,11 @@ protected Response<T> 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);
Expand Down Expand Up @@ -152,7 +154,9 @@ public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOEx

//network error
if (responseCode == 404 || responseCode >= 500) {
Response<T> error = Response.error(false, call, response, HttpException.NET_ERROR());
String message = response.message();
String string = response.body().string();
Response<T> error = Response.error(false, call, response, new HttpException(responseCode, message, string));
onError(error);
return;
}
Expand Down
22 changes: 22 additions & 0 deletions okgo/src/main/java/com/lzy/okgo/exception/HttpException.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ 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();
this.message = response.message();
this.response = response;
}


private static String getMessage(Response<?> response) {
HttpUtils.checkNotNull(response, "response == null");
return "HTTP " + response.code() + " " + response.message();
Expand All @@ -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);
}
Expand Down