我正在使用Apache Cordova WebView。在html中我使用img标签。对于此图像的HTTP请求,我想在HTTP标头中添加一个引用程序。
我尝试使用自己的CordovaWebViewClient并通过覆盖shouldInterceptRequest或shouldOverrideUrlLoading拦截:
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) {
super(cordova, view);
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Log.d("DEBUG", "AAAAAAAAAA Should intercept request" + url);
//Implement your code
return super.shouldInterceptRequest(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
Log.d("DEBUG", "AAAAAAAAAA onLoadResource" +url);
//Implement your code
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("DEBUG", "AAAAAAAAAA On page finished "+url);
//Implement your code
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("DEBUG", "AAAAAAAAAA should override url loading "+url);
//Implement your code
return super.shouldOverrideUrlLoading(view, url);
}
}
CustomCordovaWebViewClient我通过以下代码注册:
cwv = (CordovaWebView) findViewById(R.id.webView);
cwv.setWebViewClient(new CustomCordovaWebViewClient(getTabActivity(), cwv));
cwv.loadUrl("file:///android_asset/www/index.html");
任何请求都不会调用任何函数。
是否有更好的解决方案来添加http标头?
使用loadUrl(url,extraHeaders)的解决方案不适用于cordova。
我使用的是Cordova 3.6.3