liujingxing / rxhttp

🔥🔥🔥 Based on OkHttp encapsulation, support Kotlin Coroutines、RxJava2、RxJava3; 30s to get started.
https://juejin.im/post/5ded221a518825125d14a1d4
Apache License 2.0
3.75k stars 458 forks source link

自定义Parser问题 #503

Closed RookieExaminer closed 2 months ago

RookieExaminer commented 2 months ago

你好,请教一下。 rxhttp_version = '3.3.0-beta2' okhttp:4.12.0 服务器正确返回 {"code":0,"data":{}} 服务器错误返回 {"code":1,"data":"暂无对应资金池,请切换币种"}

    //自定义Parse
      override fun onParse(response: okhttp3.Response): T {
        val data: Response<T> = response.convertTo(Response::class, *types)
        var t: T? = null
        if (data.code == 0) {
            t = data.data
        }
        if (data.code != 0 || t == null ||types[0] === String::class.java) {
           //data.data为null 拿不到
            throw ParseException(data.code.toString(), data.data.toString(), response)
        }
      return t
    }

      RxHttp.postJson(url)
      .toAwaitResponseBitApi<DataBean>()
            .awaitResult {
                //正确数据
            }.onFailure {
      //提示暂无对应资金池,请切换币种
                 val src = it?.message
                Toaster.show(src)
            }

报错:rxhttp.wrapper.exception.ParseException: code is 1, null

服务器正确时是可以拿到data的,解析成DataBean。 怎么能在服务器返回data类型为string时,也可以拿到data数据

liujingxing commented 2 months ago

这是什么神仙操作,只能手动分两步解析了,如下:

    override fun onParse(response: okhttp3.Response): T {
        val responseStr: Response<String> = response.convertTo(Response::class, String::class.java)
        if (responseStr.code == 0) {
            return GsonUtil.fromJson<T>(responseStr.data, types[0])
        } else {
            throw ParseException(responseStr.code.toString(), responseStr.data, response)
        }
    }
RookieExaminer commented 2 months ago

responseStr.cod=1 可解析 data string类型

responseStr.code=0报错 解析data为 DataBean 对象时报错

2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at rxhttp.wrapper.utils.GsonUtil.fromJson(GsonUtil.java:59) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at com.azheng.coinkeep.net.converter.ResponseParserBitApi.onParse(ResponseParserBitApi.kt:65) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at rxhttp.wrapper.utils.Utils$await$2$2.onResponse(Utils.kt:37) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2024-08-06 11:03:52.279 31523-31523 RxHttp com.coinkeep.wallet.new E at java.lang.Thread.run(Thread.java:919) 2024-08-06 11:03:52.280 31523-31523 Toaster com.coinkeep.wallet.new I (QuickExchangeFragment.kt:425) The string 'null' could not be deserialized to class com.azheng.coinkeep.bean.DataBean object

image

rxhttp.wrapper.utils.GsonUtil

@NotNull public static T fromJson(String json, Type type) { Gson gson = buildGson(); T t = gson.fromJson(json, type); if (t == null) { throw new JsonSyntaxException("The string '" + json + "' could not be deserialized to " + type + " object"); } return t; }

RookieExaminer commented 2 months ago

不好意思,还是没有解决

liujingxing commented 2 months ago

已经提示的很清楚了,fronJson传入了null

RookieExaminer commented 2 months ago

实际上data是有数据的,只不过解析错误了。 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 21 path $.data

image

liujingxing commented 2 months ago

请copy我贴出的代码

RookieExaminer commented 2 months ago

image 用的就是这个代码

RookieExaminer commented 2 months ago

code=0 data不是string类型 {"code":0,"data":{}} code 1 data是string类型 {"code":1,"data":"暂无对应资金池,请切换币种"}

RookieExaminer commented 2 months ago

感谢,解决了 原因是设置了全局转换器问题 设置为非框架转换器了 setConverter(GsonConverter.create(GsonUtils.getGson()))