更改 WebView 中显示的数据
对于我的一个项目,我试图在网络视图中显示从互联网获取的页面,但在这样做之前对其进行了更改。我对从该网络视图单击的所有网址执行相同的操作。
为了实现这一点,我首先创建了一个扩展 WebView 的类,并添加了一个方法 loadAlteredUrl(String url)
public void loadAlteredUrl(String url) {
String page = getURLContent(url);
String filtered;
// Code for altering the webpage
super.loadData(filtered, "text/html", "UTF-8");
//super.loadUrl("data:text/html;UTF-8," + filtered);
}
private String getURLContent(String url)
{
try{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
return result;
} catch (IOException ex) {
return "IOEXCEPTION";
}
此代码已经过测试并且运行良好。但现在我只能改变加载的第一页,这不是我想要的。因此,我扩展了 WebViewClient 类并重载了 shouldOverloadUrl 函数:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
EnhancedWebview myview = (EnhancedWebview) view;
if(!url.contains("http://")){
if(url.substring(0, 1).equals("/"))
url = url.substring(1);
myview.loadAlteredUrl(MainWindow.baseUrl + url);
return true;
}
return false;
}
然而,奇怪的是,这段代码只在一半的情况下有效。当我调试此代码时,它总是执行相同的步骤,但由于某些奇怪的原因,只有当我实际双击或多次双击它时,它才会打开该网址。我有一种感觉,双击(甚至在必要时点击垃圾邮件)会“破坏”内部工作的某些部分,从而使其正常工作,因为当我按照应有的方式使用它时,它会执行所有必要的步骤,但不会t 加载页面(即使页面是使用我的自定义方法等获取的)。
有人知道我在这里缺少什么吗?
For one of my projects I am trying to display a page I fetch from the internet in a webview, but altering it before I do so. And I'm doing the same for all urls clicked from that webview.
To accomplish this I've first made a class that extends WebView, and added a method loadAlteredUrl(String url)
public void loadAlteredUrl(String url) {
String page = getURLContent(url);
String filtered;
// Code for altering the webpage
super.loadData(filtered, "text/html", "UTF-8");
//super.loadUrl("data:text/html;UTF-8," + filtered);
}
private String getURLContent(String url)
{
try{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
return result;
} catch (IOException ex) {
return "IOEXCEPTION";
}
This code has been tested and works great. But now I could only alter my first page loaded, and this was not what I wanted. So I extended the WebViewClient class and overloaded the shouldOverloadUrl function:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
EnhancedWebview myview = (EnhancedWebview) view;
if(!url.contains("http://")){
if(url.substring(0, 1).equals("/"))
url = url.substring(1);
myview.loadAlteredUrl(MainWindow.baseUrl + url);
return true;
}
return false;
}
The weird this is however, this code only works half of the time. When I debug this code, it always goes through the same steps, but for some weird reason, it will only open the url's if I actually doubleclick it or more. I have the feeling doubleclicking (or even spam clicking when necesarry) "breaks" something of the inner working which causes it to work correctly, because when I use it as it should be used, it goes through all the necessary steps, but doesn't load the page (even though the page is fetched using my custom methods etc).
Anyone have any idea what im missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是“不完整”链接没有调用shouldOverrrideUrlLoading()。这样做是出于安全原因。通常,您可以通过强制链接“完整”来解决此问题:
loadDataWithBaseURL 将基本 url 添加到所有不完整的链接。
The problem is that 'uncomplete' links are not calling shouldOverrrideUrlLoading(). This is done because of security reasons. Normally you can fix this by forcing the links to be 'complete':
The loadDataWithBaseURL adds the base url to all the links that aren't complete.