Code Monkey home page Code Monkey logo

Comments (3)

traywangjun avatar traywangjun commented on June 23, 2024

刚发现了 是okhttp版本的问题,我把demo里的okhttp改为4.11.0就会出现

from easyhttp.

getActivity avatar getActivity commented on June 23, 2024

小伙子,我对这个问题进行了实践,确实存在这个问题,具体问题原因是 HttpData 定义了 Headers 类型的字段,即名为 responseHeaders 字段,因为 Gson 内部无法对这种类型进行序列化和反序列化,所以出现解析报错,旧版本之所以没有这个问题,是因为旧版本的 OkHttp 是用 Java 语言写的,而到了新版本,则是用 Kotlin 语言写的,解决这个问题的方法也很简单,将 responseHeaders 字段类型从 Headers 修改成 Map 类型,具体的代码改动如下:

public class HttpData<T> {

    /** 响应头 */
    @Nullable
    private Map<String, String> responseHeaders;

    public void setResponseHeaders(@Nullable Map<String, String> responseHeaders) {
        this.responseHeaders = responseHeaders;
    }

    @Nullable
    public Map<String, String> getResponseHeaders() {
        return responseHeaders;
    }
    ......
}
public final class RequestHandler implements IRequestHandler {

    ......

    @NonNull
    @Override
    public Object requestSuccess(@NonNull HttpRequest<?> httpRequest, @NonNull Response response, @NonNull Type type) throws Throwable {

        ......

        if (result instanceof HttpData) {
            HttpData<?> model = (HttpData<?>) result;
            Headers headers = response.headers();
            int headersSize = headers.size();
            Map<String, String> headersMap = new HashMap<>(headersSize);
            for (int i = 0; i < headersSize; i++) {
                headersMap.put(headers.name(i), headers.value(i));
            }
            model.setResponseHeaders(headersMap);

            ......
        }
        return result;
    }
}

from easyhttp.

traywangjun avatar traywangjun commented on June 23, 2024

验证了下确实可以了

from easyhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.