如何创建 XWalk WebResourceResponse?
How to create XWalkWebResourceResponse?
我正在寻找创建 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 有 public 构造函数 new XWalkWebResourceResponse(bridge)
使用桥对象,尽管我没有找到检索特定桥的方法(org.xwalk.core.internal.XWalkWebResourceResponseBridge)。
在哪里可以检索 XWalkWebResourceResponseBridge?
提前致谢。
2016 年 4 月 25 日更新:
根据 可以使用 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);
}
}
感谢使用人行横道。
要创建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 文档中看到这个。
我正在寻找创建 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 有 public 构造函数 new XWalkWebResourceResponse(bridge)
使用桥对象,尽管我没有找到检索特定桥的方法(org.xwalk.core.internal.XWalkWebResourceResponseBridge)。
在哪里可以检索 XWalkWebResourceResponseBridge?
提前致谢。
2016 年 4 月 25 日更新:
根据
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);
}
}
感谢使用人行横道。
要创建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 文档中看到这个。