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

返回OkResponse<T>问题 #450

Closed
liujingxing opened this issue Jul 4, 2023 · 1 comment
Closed

返回OkResponse<T>问题 #450

liujingxing opened this issue Jul 4, 2023 · 1 comment

Comments

@liujingxing
Copy link
Owner

liujingxing commented Jul 4, 2023

对于有且仅有一个范型的解析器,可通过toXxx<OkResponse<T>>方法同时获取响应头和实体类等信息

但对于没有泛型的解析器或者有多个泛型的解析器,就没办法通过这个方式去获取,如下:

//无泛型
@rxhttp.wrapper.annotation.Parser(name = "User")
class UserParser() : Parser<User> {

    override fun onParse(response: Response): User {
        TODO("Not yet implemented")
    }
}
//两个范型
@Parser(name = "DoubleType")
public class DoubleTypeParser<F, S> extends TypeParser<Pair<F, S>> {

    protected DoubleTypeParser() {
        super();
    }

    public DoubleTypeParser(Type fType, Type sType) {
        super(fType, sType);
    }

    @Override
    public Pair<F, S> onParse(@NotNull okhttp3.Response response) throws IOException {
        Type pairType = ParameterizedTypeImpl.getParameterized(Pair.class, types);
        Response<Pair<F, S>> data = Converter.convertTo(response, Response.class, pairType);
        Pair<F, S> t = data.getData(); //获取data字段
        if (data.getCode() != 0 || t == null) {//code不等于0,说明数据不正确,抛出异常
            throw new ParseException(String.valueOf(data.getCode()), data.getMsg(), response);
        }
        return t;
    }
}

此时就会生成toXxxUsertoXxxDoubleType系列方法,无法获取OkResponse<User>OkResponse<Pair<F, S>>对象

下载也一样,目前下载只返回String或Uri对象,如果想获取OkResponse<String>OkResponse<Uri>对象,就做不到

@liujingxing
Copy link
Owner Author

使用toObservableOkResponse、toAwaitOkResponse、toFlowOkResponse替代toObservable<OkResponse<T>>、toAwait<OkResponse<T>>、toFlow<OkResponse<T>>方法,需要在toXxx方法后调用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant