哪个是从webview获取HTML代码的最简单方法?我已经尝试了stackoverflow和google的几种方法,但找不到确切的方法.请提一下确切的方法.
public class htmldecoder extends Activity implements OnClickListener,TextWatcher
{
TextView txturl;
Button btgo;
WebView wvbrowser;
TextView txtcode;
ImageButton btcode;
LinearLayout llayout;
int flagbtcode;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.htmldecoder);
txturl=(TextView)findViewById(R.id.txturl);
btgo=(Button)findViewById(R.id.btgo);
btgo.setOnClickListener(this);
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.setWebViewClient(new HelloWebViewClient());
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.getSettings().setPluginsEnabled(true);
wvbrowser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wvbrowser.addJavascriptInterface(new MyJavaScriptInterface(),"HTMLOUT");
//wvbrowser.loadUrl("http://www.google.com");
wvbrowser.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
txtcode=(TextView)findViewById(R.id.txtcode);
txtcode.addTextChangedListener(this);
btcode=(ImageButton)findViewById(R.id.btcode);
btcode.setOnClickListener(this);
}
public void onClick(View v)
{
if(btgo==v)
{
String url=txturl.getText().toString();
if(!txturl.getText().toString().contains("http://"))
{
url="http://"+url;
}
wvbrowser.loadUrl(url);
//wvbrowser.loadData("<html><head></head><body><div style='width:100px;height:100px;border:1px red solid;'></div></body></html>","text/html","utf-8");
}
else if(btcode==v)
{
ViewGroup.LayoutParams params1=wvbrowser.getLayoutParams();
ViewGroup.LayoutParams params2=txtcode.getLayoutParams();
if(flagbtcode==1)
{
params1.height=200;
params2.height=220;
flagbtcode=0;
//txtcode.setText(wvbrowser.getContentDescription()); …Run Code Online (Sandbox Code Playgroud) 如何检索当前在WebView中显示的所有HTML内容?
我发现WebView.loadData()但我找不到相反的等价物(例如WebView.getData())
请注意,我感兴趣的检索的网页,我无法控制的数据(即我不能注入Javascript函数到这些网页,这样它会调用JavaScript接口的WebView).
我需要知道这是怎么回事shouldinterceptrequest.我不知道如何创建和处理此方法来读取和替换CSS链接.谢谢!
我在HTML中创建一个非常简单的表单,在Android中使用webview查看,使用文本框输入你的名字,当你点击按钮时,它会将它显示在一个段落中,并使用html和javascript进行制作.这是我的HTML代码:
<!DOCTYPE html>
<html>
<body>
<p> Write your name and win your favorite game console name and win it! The winners will be announced in 4 days.</p>
Type your name here: <input id="thebox" type="text" name="value" value=""><br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("thebox").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
新编辑表格
<form name="albert" action="" method="POST">
<label for="firstname"> First Name </label>
<br /><br />
<input type="text" name="firstname" id="firstname" />
<input type="submit" name="sbumit" value="Submit" />
</form> …Run Code Online (Sandbox Code Playgroud) 我有一个WebView包含表格的html(帖子).单击某个提交按钮时,我会收到JSON响应.
我怎样才能获得这个JSON?
如果我不拦截请求,json会显示在webview上,所以我想我应该使用shouldInterceptRequest(我使用的是API 12),但我不知道如何获取json.
或者也许有更好的方法,比如拦截响应而不是请求?
mWebView.loadUrl(myURL);
isPageLoaded = false; // used because the url is the same for the response
mWebView.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
if(isPageLoaded){
// get the json
return null;
}
else return super.shouldInterceptRequest(view, url);
}
public void onPageFinished(WebView view, String url) {
isPageLoaded = true;
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢
我需要保存我在WebView上显示的数据.我已经通过保存在WebViewClient中创建的所有URL请求并将它们发送到后端服务来实现这一点.后端服务稍后会请求这些文件.
问题是WebViewClient正在运行自己的循环并且响应不可见,这导致我对每个资源发出两个请求.
有没有办法直接在WebViewClient中获取数据?
这是一段当前的代码,它正在运行:
private class Client extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
plainText = //get plain text and save it
webView.loadDataWithBaseURL(url, plainText, MIME_TYPE_TEXT_HTML, UTF_8_ENCODING, null);
return true;
}
@SuppressWarnings("deprecation")
@Override
public synchronized WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// we need to make sure it does nothing in this one for >= lollipop as in lollipop the call goes through both methods
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return interceptRequest(view, url, null);
}
return null;
} …Run Code Online (Sandbox Code Playgroud)