简体   繁体   中英

XWalkResourceClient.shouldInterceptLoadRequest seems not working properly

I'm trying to override url loading. Custom http requests are implemented through okhttp library. App seems starting fine but right after start it closes. I've double checked that returned mime-type set as text/html and so on. So where is a problem? Here is my code:

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;
    }
}

OK. Seems answer was simple. I just need to check to null this expression: okHttpResponse.body().contentType().charset()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM