如果重要的话:
可能阻止WebView加载图标 允许它在获得404响应后加载其他链接?
webView.setWebViewClient(new WebViewClient() {
...@Override onReceivedError...
@SuppressWarnings("deprecation")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
return getNewResponse(url);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
return getNewResponse(url);
}
private WebResourceResponse getNewResponse(String url) {
try {
OkHttpClient client = MyOkHttpClient.getInstance();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url.trim())
.addHeader("api-key", Constants.API_KEY)
.addHeader("platform", Constants.PLATFORM);
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
return new WebResourceResponse(
getMimeType(url),
response.header("content-encoding", "utf-8"),
response.body().byteStream()
);
} catch (Exception e) {
return null;
}
}
private String getMimeType(String url) {
...
return type;
}
});
答案 0 :(得分:0)
是的,你可以这样做。但它不适用于棒棒糖前设备。 首先创建一个不可见的虚拟图标,然后将该图标粘贴到您的资源文件夹中。现在,只要您获得了可以显示它的图标,就会显示以下代码,如果没有,那么您的资产文件夹会加载虚拟图标。
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if(!request.isForMainFrame() && request.getUrl().getPath().equals("/favicon.ico")) {
try {
return new WebResourceResponse("image/png", null, new BufferedInputStream(view.getContext().getAssets().open("dummyfavicon.ico")));
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}