Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义Parser遇到data=null时,会直接抛异常 #333

Closed
ppg408331701 opened this issue Dec 2, 2021 · 12 comments
Closed

自定义Parser遇到data=null时,会直接抛异常 #333

ppg408331701 opened this issue Dec 2, 2021 · 12 comments
Labels
parser parser related

Comments

@ppg408331701
Copy link

ppg408331701 commented Dec 2, 2021

自定义parser里面 Converter.convertTo()遇到data=null的情况时,
会抛出 java.io.EOFException Request end Log printing failed 的异常

 public T onParse(@NotNull okhttp3.Response response) throws IOException {
        Response<T> data = Converter.convertTo(response, Response.class, types);//这一步直接抛出异常
        T t = data.getData(); //获取data字段
        if (t == null && types[0] == String.class) {
            t = (T) data.getMsg();
        }
        if (data.getCode() != 0 || t == null) {//code不等于0,说明数据不正确,抛出异常
            throw new ParseException(String.valueOf(data.getCode()), data.getMsg(), response);
        }
        return t;
    }

Response.class的定义如下

private int code;
private int httpCode;
private String message;
private T data;

返回的数据如下
{"code":0,"data":null,"message":"","now":"2021-12-02 08:36:29"}

请问这种情况如何自定义Parser规避好?

异常如下

java.io.EOFException
        at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
        at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
        at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
        at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
        at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
        at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
        at rxhttp.wrapper.utils.LogUtil.response2Str(LogUtil.java:308)
        at rxhttp.wrapper.utils.LogUtil.log(LogUtil.java:182)
        at rxhttp.wrapper.utils.Converter.convert(Converter.kt:43)
        at rxhttp.wrapper.parse.SimpleParser.onParse(SimpleParser.kt:20)
        at rxhttp.wrapper.utils.CallKt$await$4$2.onResponse(Call.kt:44)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:929)
2021-12-02 16:33:35.171 18297-18297/com.gxrcw.activity W/System.err:     at rxhttp.wrapper.converter.GsonConverter.convert(GsonConverter.java:51)
2021-12-02 16:33:35.171 18297-18297/com.gxrcw.activity W/System.err:     at rxhttp.wrapper.utils.Converter.convert(Converter.kt:45)
2021-12-02 16:33:35.171 18297-18297/com.gxrcw.activity W/System.err:     at rxhttp.wrapper.parse.SimpleParser.onParse(SimpleParser.kt:20)
2021-12-02 16:33:35.171 18297-18297/com.gxrcw.activity W/System.err:     at rxhttp.wrapper.utils.CallKt$await$4$2.onResponse(Call.kt:44)
@ppg408331701
Copy link
Author

ppg408331701 commented Dec 2, 2021

不对,应该不是rxhttp的问题,而是okttp的问题
我看IOS是收到数据的。
而android却一直在抛异常

 private static String response2Str(Response response) throws IOException {
        ResponseBody body = OkHttpCompat.requireBody(response);
        boolean onResultDecoder = OkHttpCompat.needDecodeResult(response);

        BufferedSource source = body.source();
        source.request(Long.MAX_VALUE); // Buffer the entire body.  //异常在这里被抛出
        Buffer buffer = source.buffer();
        String result;
        if (isProbablyUtf8(buffer)) {
            result = buffer.clone().readString(getCharset(body));
            if (onResultDecoder) {
                result = RxHttpPlugins.onResultDecoder(result);
            }
        } else {
            result = "(binary " + buffer.size() + "-byte body omitted)";
        }
        return result;
    }

@liujingxing
Copy link
Owner

必现的吗?是的话,私聊我,327744707

@ppg408331701
Copy link
Author

ppg408331701 commented Dec 3, 2021

必现的吗?是的话,私聊我,327744707

目前能确定的是服务器那边由于某个条件判断失败了,导致的返回的数据在android上接受到但解析失败。
但是在ios和 浏览器的控制台可以解析出来。

后端是.net 在返回数据用的某个方法似乎会让source.request(Long.MAX_VALUE)抛出异常。

后端自己也不知道为什么,换了一个方法后解决了

@liujingxing
Copy link
Owner

用的rxhttp版本多少,日志打印失败按道理是不会影响请求的

@ppg408331701
Copy link
Author

ppg408331701 commented Dec 3, 2021

后端的描述是,当判断业务错误时。他会直接在http的响应头里中断。导致http连接提前中断了

okhttp就会抛出异常

@ppg408331701
Copy link
Author

ppg408331701 commented Dec 3, 2021

square/okio#286
square/okhttp#3113

有点类似这个,我昨天差到的,JakeWharton认为这个是正确的行为

版本号是rxhttp/2.8.0 okhttp/4.9.1

@liujingxing
Copy link
Owner

看了下代码,确定有try catch语句,这个异常会导致app崩溃?

@ppg408331701
Copy link
Author

看了下代码,确定有try catch语句,这个异常会导致app崩溃?

不会崩溃,只是捕捉到java.io.EOFException这个异常。但是ios又能解析出来,android解析不出来,所以昨天挠了一天的头

@liujingxing
Copy link
Owner

那这个只是日志打印异常,应该还有另外一个异常,有看过失败回调是什么异常吗?

@ppg408331701
Copy link
Author

ppg408331701 commented Dec 3, 2021

image
java.io.EOFException
at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
at rxhttp.wrapper.utils.LogUtil.response2Str(LogUtil.java:308)
at rxhttp.wrapper.utils.LogUtil.log(LogUtil.java:182)
at rxhttp.wrapper.utils.Converter.convert(Converter.kt:43)
at rxhttp.wrapper.parse.SimpleParser.onParse(SimpleParser.kt:20)
at rxhttp.wrapper.utils.CallKt$await$4$2.onResponse(Call.kt:44)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:929)

logcat就是显示这个异常

也许你可以参考square/retrofit#1554 这个

@ppg408331701
Copy link
Author

我找到了这篇文章,应该就是这个描述的样子。
后端主动断开了和app的http连接导致的
https://www.codeleading.com/article/2064419567/

@liujingxing
Copy link
Owner

好的,我看看

@liujingxing liujingxing added the parser parser related label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser parser related
Projects
None yet
Development

No branches or pull requests

2 participants