Skip to content

Commit

Permalink
修改错误返回
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicent9920 committed May 6, 2020
1 parent 765f73f commit d64a1e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/captures
.externalNativeBuild
.cxx
.idea/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
16 changes: 14 additions & 2 deletions rxhttp_kt/src/main/java/per/goweii/rxhttp/kt/request/RxRequest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package per.goweii.rxhttp.kt.request

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
Expand Down Expand Up @@ -67,6 +70,7 @@ class RxRequest<T, E> where E : BaseResponse<T> {
}

fun request(callback: ResultCallback<T>): Disposable {

val disposable = mObservable!!.subscribe({ bean ->
when {
bean == null -> {
Expand All @@ -89,8 +93,16 @@ class RxRequest<T, E> where E : BaseResponse<T> {
mListener?.onError(handle)
}
if(t is HttpException){
if(RxHttp.getRequestSetting()?.getMultiHttpCode()?.invoke(t.code()) == false){
callback.onFailed(t.code(), "${t.message}")
if(RxHttp.getRequestSetting()?.getMultiHttpCode()?.invoke(t.code()) == true){
try {
val msg = t.response()?.errorBody()?.string()
val bean = Gson().fromJson<BaseResponse<T>>(msg, object :TypeToken<BaseResponse<T>>(){}.type)
callback.onFailed(t.code(), bean.getMsg()?:"${t.message}")
} catch (e: Exception) {
callback.onFailed(t.code(), "${t.message}")
}


}
}else{
callback.onFailed(-2, "其它异常:${t.message}")
Expand Down

0 comments on commit d64a1e9

Please sign in to comment.