|
@@ -37,9 +37,9 @@ import java.lang.reflect.Type
|
|
|
* @param message 错误信息在JSON中的字段名
|
|
|
*/
|
|
|
abstract class JSONConvert(
|
|
|
- val success: String = "0",
|
|
|
- val code: String = "code",
|
|
|
- val message: String = "msg"
|
|
|
+ val success: String = "0",
|
|
|
+ val code: String = "code",
|
|
|
+ val message: String = "msg"
|
|
|
) : NetConverter {
|
|
|
|
|
|
override fun <R> onConvert(succeed: Type, response: Response): R? {
|
|
@@ -52,11 +52,12 @@ abstract class JSONConvert(
|
|
|
val bodyString = response.body?.string() ?: return null
|
|
|
return try {
|
|
|
val json = JSONObject(bodyString) // 获取JSON中后端定义的错误码和错误信息
|
|
|
- if (json.getString(this.code) == success) { // 对比后端自定义错误码
|
|
|
+ val srvCode = json.getString(this.code)
|
|
|
+ if (srvCode == success) { // 对比后端自定义错误码
|
|
|
bodyString.parseBody<R>(succeed)
|
|
|
} else { // 错误码匹配失败, 开始写入错误异常
|
|
|
val errorMessage = json.optString(message, NetConfig.app.getString(com.drake.net.R.string.no_error_message))
|
|
|
- throw ResponseException(response, errorMessage)
|
|
|
+ throw ResponseException(response, errorMessage, tag = srvCode) // 将业务错误码作为tag传递
|
|
|
}
|
|
|
} catch (e: JSONException) { // 固定格式JSON分析失败直接解析JSON
|
|
|
bodyString.parseBody<R>(succeed)
|