Skip to content

Commit

Permalink
修改所有请求方法,增加真实错误信息是否返回的判断
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicent9920 committed Nov 17, 2020
1 parent ac91fe6 commit 71743cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- 支持解析过程自定义(如`responseBody` \ `SuccessCode` 等自定义处理)
- 支持Cookie管理
- 增加网络取消

- 文件下载(`RxDownload`
- 支持断点续传
- 支持下载进度回调
Expand Down
24 changes: 19 additions & 5 deletions rxhttp_kt/src/main/java/per/goweii/rxhttp/kt/request/RxRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import retrofit2.HttpException
* <p>版本号:1<p>
*
*/
class RxRequest<T, E> where E : BaseResponse<T> {
class RxRequest<T, E> private constructor(
observable: Observable<E>?,
observable2: Observable<T>?
) where E : BaseResponse<T> {

private var mListener: RequestListener? = null
private var mRxLife: RxLife? = null
Expand All @@ -40,7 +43,7 @@ class RxRequest<T, E> where E : BaseResponse<T> {
private var mObservable: Observable<E>? = null
private var mObservable2: Observable<T>? = null

private constructor(observable: Observable<E>?, observable2: Observable<T>?) {
init {
if (observable2 == null) {
this.mObservable =
observable!!.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
Expand Down Expand Up @@ -89,8 +92,13 @@ class RxRequest<T, E> where E : BaseResponse<T> {
if (t is HttpException) {
if (RxHttp.getRequestSetting()?.getMultiHttpCode()?.invoke(t.code()) == false) {
mListener?.onError(handle)
// val errorMsg = t.response()?.errorBody()?.string()
callback.onFailed(t.code(), t.message())
if(RxHttp.getRequestSetting()?.getRealErrorMsg() == true){
val errorMsg = t.response()?.errorBody()?.string()
callback.onFailed(t.code(), errorMsg?:t.message())
}else{
callback.onFailed(t.code(), t.message())
}

} else
mListener?.onFinish()

Expand Down Expand Up @@ -120,6 +128,7 @@ class RxRequest<T, E> where E : BaseResponse<T> {
// 无法获取实体,建议直接返回字符串
if (RxHttp.getRequestSetting()?.getMultiHttpCode()?.invoke(t.code()) == false) {
mListener?.onError(handle)
val realMsg = RxHttp.getRequestSetting()?.getRealErrorMsg()?:false
val errorMsg = t.response()?.errorBody()?.string()
val result = object : BaseResponse<T>() {
override fun getCode(): Int {
Expand All @@ -139,7 +148,12 @@ class RxRequest<T, E> where E : BaseResponse<T> {
}

override fun getMsg(): String? {
return errorMsg ?: t.message()
return if(realMsg){
errorMsg ?: t.message()
}else{
t.message()
}

}

override fun setMsg(msg: String?) {
Expand Down

0 comments on commit 71743cf

Please sign in to comment.