Skip to content

Commit

Permalink
提供获取网络请求错误码
Browse files Browse the repository at this point in the history
  • Loading branch information
Reginer committed Dec 29, 2021
1 parent 88f4745 commit bb699d3
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ maven { url 'https://jitpack.io' }
```

```
implementation 'com.github.Reginer:MVVMHub:2.1.1'
implementation 'com.github.Reginer:MVVMHub:2.1.2'
```

登录:
Expand All @@ -31,7 +31,7 @@ class LoginActivity : BaseVmActivity() {
mViewModel.loginResult.vmObserver(this) {
onAppLoading { showProgress() }
onAppSuccess { mViewModel.saveUser(it);finish() }
onAppError { Logger.e(it.errorMsg) }
onAppError { Logger.e(it.errorMsg);Logger.e(" error code is:::" + it.errorCode) }
onAppComplete { dismissProgress() }
}
//不管那一套,直接取成功就完事了
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ dependencies {
androidTestImplementation(MvvmExt.espresso)
implementation project(path: ':base')
implementation(MvvmExt.viewBinding)
// releaseImplementation 'com.github.Reginer:MVVMHub:2.1.1'
// releaseImplementation 'com.github.Reginer:MVVMHub:2.1.2'
}
43 changes: 35 additions & 8 deletions app/src/main/java/win/regin/mvvm/api/NetworkApi.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2019, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.api

import com.google.gson.GsonBuilder
Expand All @@ -12,9 +43,9 @@ import win.regin.mvvm.model.Urls


/**
* @author :Reginer in 2019/6/18 20:49.
* @author :Reginer 2019/6/18 20:49.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:网络请求
*/
object NetworkApi {
fun getApi(): NetApiService {
Expand All @@ -36,13 +67,9 @@ object NetworkApi {
* 配置拦截器
*/
private fun getInterceptor(): Interceptor {
val rLog = object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Logger.i(message)
}
}
val rLog = HttpLoggingInterceptor.Logger { Logger.i(it) }
return HttpLoggingInterceptor(rLog).apply {
level = HttpLoggingInterceptor.Level.NONE
level = HttpLoggingInterceptor.Level.BODY
}
}
}
Expand Down
39 changes: 37 additions & 2 deletions app/src/main/java/win/regin/mvvm/data/AppBaseEntity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
/*
* Copyright (c) 2019, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.mvvm.data

import win.regin.common.BaseEntity
import win.regin.mvvm.constant.HubConstant

/**
* @author :Reginer in 2019/11/21 20:57.
* @author :Reginer 2019/11/21 20:57.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:接口实体类封装
*/

class AppBaseEntity<T>(private var errorCode: Int, private var errorMsg: String, data: T) :
Expand All @@ -21,4 +52,8 @@ class AppBaseEntity<T>(private var errorCode: Int, private var errorMsg: String,
return errorMsg
}

override fun getResCode(): Int {
return errorCode
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/win/regin/mvvm/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LoginActivity : BaseVmActivity() {
mViewModel.loginResult.vmObserver(this) {
onAppLoading { showProgress() }
onAppSuccess { mViewModel.saveUser(it);finish() }
onAppError { Logger.e(it.errorMsg) }
onAppError { Logger.e(it.errorMsg);Logger.e(" error code is:::" + it.errorCode) }
onAppComplete { dismissProgress() }
}
//不管那一套,直接取成功就完事了
Expand Down
41 changes: 37 additions & 4 deletions base/src/main/java/win/regin/base/exception/AppException.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
/*
* Copyright (c) 2019, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.base.exception

import win.regin.base.ext.parseErrorString

/**
* @author :Reginer in 2019/7/8 9:41.
* @author :Reginer 2019/7/8 9:41.
* 联系方式:QQ:282921012
* 功能描述:错误
* 功能描述:错误类封装
*/

class AppException : Exception {
var errorMsg: String

constructor(error: String?) : super(error) {
errorMsg = error ?: parseError(null)
var errorCode: Int = -1

constructor(message: String?, code: Int) : super(message) {
errorMsg = message ?: parseError(null);errorCode = code
}

constructor(throwable: Throwable?) : super(throwable) {
Expand Down
37 changes: 34 additions & 3 deletions base/src/main/java/win/regin/base/ext/MvvmExt.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
/*
* Copyright (c) 2021, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.base.ext

import androidx.annotation.MainThread
Expand All @@ -12,9 +43,9 @@ import java.net.ConnectException
import java.net.UnknownHostException

/**
* @author :Reginer in 19-6-18 下午6:04.
* @author :Reginer 19-6-18 下午6:04.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:扩展类
*/


Expand Down Expand Up @@ -94,7 +125,7 @@ fun BaseViewModel.launchBlock(block: () -> Unit) {
*/
fun <T> VmLiveData<T>.paresVmResult(result: BaseEntity<T>) {
value = if (result.dataRight()) VmState.Success(result.data) else
VmState.Error(AppException(result.getMsg()))
VmState.Error(AppException(result.getMsg(), result.getResCode()))
}


Expand Down
42 changes: 40 additions & 2 deletions base/src/main/java/win/regin/common/BaseEntity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
/*
* Copyright (c) 2019, Reginer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package win.regin.common

import androidx.annotation.Keep

/**
* @author :Reginer in 19-6-19 下午6:49.
* @author :Reginer 19-6-19 下午6:49.
* 联系方式:QQ:282921012
* 功能描述:
* 功能描述:接口请求基类
*/
@Keep
open class BaseEntity<T>(
Expand All @@ -30,6 +61,13 @@ open class BaseEntity<T>(
open fun getMsg(): String {
return message
}

/**
* 获取状态码
*/
open fun getResCode(): Int {
return code
}
}


4 changes: 2 additions & 2 deletions buildSrc/src/main/java/MvvmExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ object Version {
const val okhttp3LogVersion = "4.9.1"
const val materialVersion = "1.4.0"
const val loggerVersion = "2.2.0"
const val libVersionCode = 30
const val libVersionName = "2.1.1"
const val libVersionCode = 31
const val libVersionName = "2.1.2"
}

object MvvmExt {
Expand Down

0 comments on commit bb699d3

Please sign in to comment.