我正在做一个项目,我们有兴趣从 WebView 拦截一些 HTTP 流量,然后将一些额外的 header 附加到请求,然后返回并在 WebView 中显示它。
为此,我们使用 shouldInterceptRequest(WebView view, String URL) 方法,它返回 webview 应该显示的 WebResourceResponse。
这是我的做法:
private WebResourceResponse handleRequest(String url){
Log.d(TAG, "Intercepting request at : " + url);
HttpURLConnection connection = null;
URL serverAddress = null;
WebResourceResponse response = null;
InputStream is = null;
String type = null;
String encoding = null;
int statusCode;
int length;
try
{
//Set up the initial connection
serverAddress = new URL(url);
connection = (HttpURLConnection)serverAddress.openConnection();
// Initiate a HEAD request to get meta data
connection.setRequestMethod("HEAD");
// Let the WebView handle requests with no headers in them.
Map<String, List<String>> headers = connection.getHeaderFields();
if(headers == null){
Log.d(TAG, "Retrieved response with zero headers at: " + url);
return null;
}
statusCode = connection.getResponseCode();
if(statusCode == 200){
// OK, delegate request if content length is large enough
type = connection.getContentType();
encoding = connection.getContentEncoding();
length = connection.getContentLength();
Log.d(TAG, "ContentType = " + type + " encoding = " + encoding + " " + url);
connection.disconnect();
if(length > SIZE_THRESHOLD){
// send to other device
Log.d(TAG, "Delegating request to other device NOT IMPLEMENTED");
// TODO: Currently just handling them locally.
}
else{
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod("GET");
is = connection.getInputStream();
statusCode = connection.getResponseCode();
type = connection.getContentType();
encoding = connection.getContentEncoding();
Log.d(TAG, "Done Loading " + url);
return new WebResourceResponse(type, encoding, is);
}
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (ProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "IOException when connection to : " + url + " Stacktrace: " + Log.getStackTraceString(e));
}
return response;
}
如果我尝试加载像“www.madopskrifter.nu”或“www.newz.dk”这样的页面,那么我会得到完整的内容并显示图片和所有内容。但是,如果我浏览到像 stackoverflow 这样的页面,我只会在 stackoverflow.com 上获得 HTML 索引文件。 youtube.com 也是如此。 (我确实在 WebView 设置中启用了 JavaScript)
有什么我想念的吗?目前我什至没有添加自己的标题或任何东西,我只是简单地返回相同的响应,只是我是“手动”获取它们。 (此外,我现在不处理 header 为零的请求,但即使我注释掉该代码,问题仍然存在)。
如有任何帮助,我们将不胜感激,
最佳答案
我找到了解决我遇到的同样问题的方法,YMMV:
我的 HttpUrlConnection 正在返回:
- 编码:空
- mimeType: "text/html; charset=utf-8"
这就是问题所在。
我现在检查 html 文件,并更改为:
- 编码:“UTF-8”
- mimeType: "text/html"
例子:
String responseEncoding = urlConnection.getContentEncoding();
String responseMimeType = urlConnection.getContentType();
String html = "";
if (resourceUrl.endsWith("html"))
{
responseEncoding = "UTF-8";
responseMimeType = "text/html";
}
if((responseCode == 200) && (!is.equals("")))
{
return new WebResourceResponse(responseMimeType, responseEncoding, returnStream);
} else
{
return null;
}
关于android - Webview 仅在某些页面上显示原始 HTML 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937063/