XWalkResourceClient.shouldInterceptLoadRequest似乎无法正常工作

时间:2017-01-14 05:57:56

标签: android networking crosswalk-runtime intercept crosswalk

我试图覆盖网址加载。自定义http请求通过okhttp库实现。应用似乎开始很好,但在开始关闭之后。我已经双重检查过返回的mime-type设置为text / html等等。那问题出在哪里?这是我的代码:

public class MyXWalkResourceClient extends XWalkResourceClient {
    @Override
    public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
        Map<String, String> headers = request.getRequestHeaders();
        headers.put("Accept-Language", "ru,en-US;q=0.8,en;q=0.6");

        Request okHttpRequest = new Request.Builder()
                .url(request.getUrl().toString())
                .headers(Headers.of(headers))
                .build();

        Response okHttpResponse = null;
        try {
            okHttpResponse = client.newCall(okHttpRequest).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (okHttpResponse == null) {
            return null;
        }

        XWalkWebResourceResponse resourceResponse = createXWalkWebResourceResponse(
                okHttpResponse.body().contentType().toString().split(";")[0],
                okHttpResponse.body().contentType().charset().name(),
                okHttpResponse.body().byteStream(),
                okHttpResponse.code(),
                okHttpResponse.message(),
                toRegularMap(okHttpResponse.headers().toMultimap()));
        return resourceResponse;
    }

    private Map<String, String> toRegularMap(Map<String, List<String>> multimap) {
        Map<String, String> resultMap = new HashMap<>();
        for (Entry<String, List<String>> entry : multimap.entrySet()) {
            resultMap.put(entry.getKey(), TextUtils.join(",", entry.getValue()));
        }
        return resultMap;
    }
}

1 个答案:

答案 0 :(得分:0)

行。似乎答案很简单。我只需要检查以使该表达式为空:okHttpResponse.body().contentType().charset()