Skip to content

Commit

Permalink
* 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
eajon authored and wengyijiong committed Jan 22, 2019
1 parent 160759a commit f8929d6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 83 deletions.
79 changes: 30 additions & 49 deletions RxHttp/src/main/java/com/github/eajon/exception/ApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,15 @@
*/

public class ApiException extends Exception {
//对应HTTP的状态码
private static final int BADREQUEST = 400;
private static final int UNAUTHORIZED = 401;
private static final int FORBIDDEN = 403;
private static final int NOT_FOUND = 404;
private static final int METHOD_NOT_ALLOWED = 405;
private static final int REQUEST_TIMEOUT = 408;
private static final int INTERNAL_SERVER_ERROR = 500;
private static final int BAD_GATEWAY = 502;
private static final int SERVICE_UNAVAILABLE = 503;
private static final int GATEWAY_TIMEOUT = 504;

private final int code;

private String message;


private String bodyMessage;


public ApiException(Throwable throwable, int code) {
super(throwable);
this.code = code;
Expand All @@ -68,34 +60,28 @@ public int getCode() {
return code;
}

@Override
public String getMessage() {
return message;
}

public String getBodyMessage() {
return bodyMessage;
}

public String getDisplayMessage() {
return "(HttpCode:" + code + ")"+ message ;
return "HttpCode:" + code + ";" + "throwable:" + message + ";" + "body:" + bodyMessage;
}


public static ApiException handleException(Throwable e) {
ApiException ex;
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
HttpException httpException = ( HttpException ) e;
ResponseBody responseBody = httpException.response().errorBody();
ex = new ApiException(httpException, httpException.code());
/*switch (httpException.code()) {
case BADREQUEST:
case UNAUTHORIZED:
case FORBIDDEN:
case NOT_FOUND:
case REQUEST_TIMEOUT:
case GATEWAY_TIMEOUT:
case INTERNAL_SERVER_ERROR:
case BAD_GATEWAY:
case SERVICE_UNAVAILABLE:
default:
ex.message = "网络错误,Code:"+httpException.code()+" ,err:"+httpException.getMessage();
break;
}*/

try {
ex.message = responseBody.string();
ex.bodyMessage = responseBody.string();
} catch (IOException e1) {
e1.printStackTrace();
}
Expand All @@ -108,55 +94,50 @@ public static ApiException handleException(Throwable e) {
|| e instanceof NotSerializableException
|| e instanceof ParseException) {
ex = new ApiException(e, ERROR.PARSE_ERROR);
ex.message = "解析错误";
ex.bodyMessage = "解析错误";
return ex;
} else if (e instanceof ClassCastException) {
ex = new ApiException(e, ERROR.CAST_ERROR);
ex.message = "类型转换错误";
ex.bodyMessage = "类型转换错误";
return ex;
} else if (e instanceof ConnectException) {
ex = new ApiException(e, ERROR.NETWORD_ERROR);
ex.message = "连接失败";
ex = new ApiException(e, ERROR.NETWORK_ERROR);
ex.bodyMessage = "网络错误";
return ex;
} else if (e instanceof javax.net.ssl.SSLHandshakeException) {
ex = new ApiException(e, ERROR.SSL_ERROR);
ex.message = "证书验证失败";
ex.bodyMessage = "证书错误";
return ex;
} else if (e instanceof ConnectTimeoutException) {
ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
ex.bodyMessage = "连接超时";
return ex;
} else if (e instanceof java.net.SocketTimeoutException) {
ex = new ApiException(e, ERROR.TIMEOUT_ERROR);
ex.message = "连接超时";
ex.bodyMessage = "连接超时";
return ex;
} else if (e instanceof UnknownHostException) {
ex = new ApiException(e, ERROR.UNKNOWNHOST_ERROR);
ex.message = "无法解析该域名";
ex.bodyMessage = "未知主机错误";
return ex;
} else if (e instanceof NullPointerException) {
ex = new ApiException(e, ERROR.NULLPOINTER_EXCEPTION);
ex.message = "NullPointerException";
ex.bodyMessage = "空指针错误";
return ex;
} else {
ex = new ApiException(e, ERROR.UNKNOWN);
ex.message = "未知错误";
ex.bodyMessage = "未定义错误";
return ex;
}
}

@Override
public String getMessage() {
return message;
}


/**
* 约定异常
*/
public static class ERROR {
/**
* 未知错误
* 未定义错误
*/
public static final int UNKNOWN = 1000;
/**
Expand All @@ -166,14 +147,14 @@ public static class ERROR {
/**
* 网络错误
*/
public static final int NETWORD_ERROR = PARSE_ERROR + 1;
public static final int NETWORK_ERROR = PARSE_ERROR + 1;
/**
* 协议出错
* 协议错误
*/
public static final int HTTP_ERROR = NETWORD_ERROR + 1;
public static final int HTTP_ERROR = NETWORK_ERROR + 1;

/**
* 证书出错
* 证书错误
*/
public static final int SSL_ERROR = HTTP_ERROR + 1;

Expand Down
22 changes: 0 additions & 22 deletions RxHttp/src/main/java/com/github/eajon/util/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,4 @@ public static Object getHeaderValueEncoded(Object value) {
}
}

/**
* 获取BaseUrl
* 备注:根据完整URL获取BasUrl
*
* @param url
* @return
*/
public static String getBaseUrl(String url) {
String head = "";
int index = url.indexOf("://");
if (index != -1) {
head = url.substring(0, index + 3);
url = url.substring(index + 3);
}
index = url.indexOf("/");
if (index != -1) {
url = url.substring(0, index + 1);
}
return head + url;
}


}
40 changes: 28 additions & 12 deletions app/src/main/java/com/eajon/my/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,19 @@ protected void initLogic() {

private void doRequest() {
HashMap map = new HashMap();
map.put("username", "admin");
map.put("password", "12345678");
map.put("id", 4L);
new RxHttp.Builder()
.post()
.apiUrl("user/login")
.get()
.apiUrl("test/wechat/token")
.addParameter(map)
.entity(CommonResponse.class)
.build()
.request(new HttpObserver<CommonResponse>() {
.request(new HttpObserver() {
@Override
public void onSuccess(CommonResponse o) {
public void onSuccess(Object o) {
HashMap<String,Object> header=new HashMap<>();
header.put("Authorization",o.getData());
header.put("Authorization", o.toString());
RxHttp.getConfig().baseHeader(header);
content.setText(o.getData().toString());
content.setText(o.toString());
}

@Override
Expand Down Expand Up @@ -170,6 +168,25 @@ public void onError(ApiException t) {
});
}

private void doProfile() {
new RxHttp.Builder()
.get()
.apiUrl("api/user/profile")
.entity(CommonResponse.class)
.build()
.request(new HttpObserver<CommonResponse>() {
@Override
public void onSuccess(CommonResponse o) {
content.setText(o.getData().toString());
}

@Override
public void onError(ApiException t) {
LoggerUtils.error(t.getBodyMessage());
}
});
}


@RxSubscribe(observeOnThread = EventThread.IO, isSticky = true, tag = "weather")
public void weatherCallBack(Weather weather) {
Expand Down Expand Up @@ -254,7 +271,7 @@ public void onSuccess(CommonResponse o) {

@Override
public void onError(ApiException t) {
content.setText(t.getDisplayMessage());
content.setText(t.getBodyMessage());
}
});

Expand Down Expand Up @@ -330,8 +347,7 @@ public void onError(ApiException t) {
break;
case R.id.request:
// doJsonRequest();
WeatherModule2 weatherModule2 = ViewModelProviders.of(this).get(WeatherModule2.class);
weatherModule2.getWeather();
doProfile();
break;
case R.id.stick:
intent = new Intent(this, SecondActivity.class);
Expand Down

0 comments on commit f8929d6

Please sign in to comment.