如何加载javascript重定向到机器人的WebView? [英] How to load a javascript redirect into android webview?

查看:149
本文介绍了如何加载javascript重定向到机器人的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个页面加载到web视图。通常情况下,当我打电话

I am trying to load a page into webview. Normally, when i call

webview.loadUrl(url);

它工作正常。该网址有一个javascript code重定向的页​​面。这里是jacascript code:

it works fine. The url has a javascript code that redirects the page. Here is the jacascript code:

<script type="text/javascript">
        if (!document.cookie || document.cookie.indexOf('AVPDCAP=') == -1)
        { document.write('<scr'+'ipt src="http://some_link_here+Math.floor(89999999*Math.random()+10000000)+'&millis='+new Date().getTime()+'&referrer='+encodeURIComponent(document.location)+'" type="text/javascript"></scr'+'ipt>'); }
</script>

当我尝试了JavaScript code加载到我的WebView这样的:

When i try to load that javascript code into my webview like the following:

String data = javascript_code_above;
topBannerWV.loadDataWithBaseURL("", data, "text/html", "UTF-8", null);

其中数据这个JavaScript code,它不会加载该页面。我也试过以下内容:

where data is this javascript code, it does not load the page. I also tried following:

topBannerWV.loadDataWithBaseURL("", data, "text/javascript", "UTF-8", null);

但这次的文本加载到web视图。谁能帮我这个?

but this time that text is loaded into webview. Can anyone help me with this?

感谢。

推荐答案

我找到了解决办法。

而不是直接给字符串的WebView加载,我第一次写它变成一个文件,然后给该文件的WebView加载。

Instead of directly giving the string to webview to load, i first wrote it into a file and then gave that file to webview to load.

 getContext().getApplicationContext().deleteFile("data.html");
 Writer output = null;
 File dir = getContext().getApplicationContext().getFilesDir();
 File file = new File(dir.getAbsolutePath() + File.separator + "data.html");
 file.createNewFile();
 output = new BufferedWriter(new FileWriter(file));
 output.write(WVdata);
 output.close();
 topBannerWV.loadUrl("file:///" + getContext().getApplicationContext().getFilesDir() + "/data.html");

我不知道为什么会这样,其实我不改变的数据串,只是把它放到一个文件中。

I do not know why this happens, actually i do not change the data string, just put it into a file.

这篇关于如何加载javascript重定向到机器人的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆