使用Android浏览器 - 检测页面变化 [英] Using Android Browser - Detecting page change
问题描述
我有打开的浏览器到用户指定网址的应用程序。这应该指向一个没有认证一定的登录页面。是否有可能发现当用户点击登录进入会员页面,我以后可以用于解析HTML源代码?我只是在寻找某种页变化检测。
I have an app that opens up the browser to a user given url. This should point to a certain login page that has no authentication. Is it possible to detect when the user clicks login to go to the member page that I can later parse for the HTML source? I'm just looking for some kind of on page change detection.
编辑:我现在用的WebView。这里是新的WebView活动课的一部分,迄今:
I am now using WebView. Here's part of the new WebView activity class so far:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("theurl(removed)"); // Removed url for StackOverflow
mWebView.loadUrl("javascript:document.getElementById('sid').value = 'name';"); // Supposed to autofill a particular input with id 'sid'
试图在页面上输入的第一个变化值,都无济于事。
Trying to first change value of an input on the page, to no avail.
推荐答案
首先,你应该打开URL您的应用程序内,只需使用的WebView使用自定义WebViewClient:
First, you should open the url inside your app, simply by using a WebView with a custom WebViewClient:
WebView myWebView = (WebView) findViewById(R.id.mywebview);
CustomWebViewClient webViewClient = new CustomWebViewClient();
myWebView.setWebViewClient(webViewClient);
您用它打开你的网址:
myWebView.loadUrl("http://myurl.com");
private class CustomWebViewClient extends WebViewClient {
@Override
/**
* I assume the url changes when the user clicks login, so parse the url and do the right stuff
* I look for a fictitious sessionkey here
*/
public void onLoadResource(WebView view, String url) {
try {
String sessionKey = URLDecoder.decode(view.getUrl().split("session_key=")[1], "UTF-8");
if ( sessionKey == 1337)
{
...
}
}
catch (Exception e) {
e.printStackTrace();
}
super.onLoadResource(view, url);
}
@Override
/**
* Take control over the loaded url so the url is open in the current Webview and not with the default browser of Android
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
这篇关于使用Android浏览器 - 检测页面变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!