我正在寻找一种方法来创建XWalkWebResourceResponse.我的情况如下.
我正在创建自定义XWalkResourceClient客户端,它将拦截所有网络请求.所有应该被拦截的请求都会以抽象包装器的形式返回 – AppWebResourceResponse.
public final class CrossWebViewClient extends XWalkResourceClient {
// other fields and constructor
CrossWebResourceResponseMapper responseMapper;
@Override
public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
AppWebResourceRequest mappedRequest = requestMapper.toGenericRequest(request);
AppWebResourceResponse interceptedResponse = requestInterceptor.shouldInterceptRequest(mappedRequest);
if (interceptedResponse == null) {
return super.shouldInterceptLoadRequest(view, request);
}
return responseMapper.toTargetResponse(interceptedResponse);
}
}
最后一步.
我必须将AppWebResourceResponse调整为XWalkWebResourceResponse,这是我遇到问题的地方.
public class CrossWebResourceResponseMapper implements AppWebResourceResponse.Mapper<XWalkWebResourceResponse> {
@Override
public XWalkWebResourceResponse toTargetResponse(final AppWebResourceResponse appResponse) {
String mimeType = appResponse.getMimeType();
String encoding = appResponse.getEncoding();
int statusCode = appResponse.getStatusCode();
String reasonPhrase = appResponse.getReasonPhrase();
Map<String, String> headers = appResponse.getResponseHeaders();
InputStream data = appResponse.getData();
// ??? how to create response object?
XWalkWebResourceResponse response;
response.setMimeType(mimeType);
response.setEncoding(encoding);
response.setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
response.setResponseHeaders(headers);
response.setData(data);
return response;
}
}
根据API有公共构造函数新的XWalkWebResourceResponse(桥)消耗桥对象,虽然我没有找到一种方法来检索特定的桥(org.xwalk.core.internal.XWalkWebResourceResponseBridge).
我在哪里可以检索XWalkWebResourceResponseBridge?
提前致谢.
2016年4月25日更新:
根据Xiaofeng,可以使用XWalkResourceClient对象创建XWalkWebResourceResponse对象.
public class XWalkWebResourceResponseMapper {
public XWalkWebResourceResponse toTargetResponse(XWalkResourceClient client, AppWebResourceResponse appResponse) {
String mimeType = appResponse.getMimeType();
String encoding = appResponse.getEncoding();
int statusCode = appResponse.getStatusCode();
String reasonPhrase = appResponse.getReasonPhrase();
Map<String, String> headers = appResponse.getResponseHeaders();
InputStream data = appResponse.getData();
return client.createXWalkWebResourceResponse(mimeType, encoding, data, statusCode, reasonPhrase, headers);
}
}
public final class CrossWebViewClient extends XWalkResourceClient {
@Override
public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
AppWebResourceRequest mappedRequest = requestMapper.toGenericRequest(request);
AppWebResourceResponse interceptedResponse = requestInterceptor.shouldInterceptRequest(mappedRequest);
if (interceptedResponse == null) {
return super.shouldInterceptLoadRequest(view, request);
}
return responseMapper.toTargetResponse(this, interceptedResponse);
}
}
解决方法:
谢谢使用Crosswalk.
要创建XWalkWebResourceResponse,您应该在XWalkResourceClient中使用createXWalkWebResourceResponse,请参阅here.由于内部块问题,它不能使用通用构造函数来创建.
以下是有关如何使用它的示例:https://github.com/crosswalk-project/crosswalk/pull/3454/files
class MyResourceClient extends XWalkResourceClientInternal {
MyResourceClient(XWalkViewInternal view) {
super(view);
}
@Override
XWalkWebResourceResponseInternal shouldInterceptLoadRequest(XWalkViewInternal view,
XWalkWebResourceRequestInternal request) {
// Handle it here.
// Use createXWalkWebResourceResponse instead of "new XWalkWebResourceResponse"
// to create the response.
// Similar with before, there are two function to use:
// 1) createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data)
// 2) createXWalkWebResourceResponse(String mimeType, String encoding, InputStream data,
// int statusCode, String reasonPhrase, Map<String, String> responseHeaders)
...
}
}
稍后您还可以在XWalkView API文档中看到这一点.