Android WebView.loadUrl() in for loop firing before first url finished
loading
So I am having troubles loading a list of websites and collecting their
html code with Android's WebView class. I expect all pages to be
different, so I tried waiting until the HTML result differs, but I can't
seem to find the solution. I also tried this.wait()'ing my for loop, and
then have it be notified in the WebViewClient's onPageFinished method, but
that doesn't work either, it only freezes the whole application.
for (Element e : users) {
Element link = e.select("a[href]").first();
wv.loadUrl(link.attr("href"));
while (result.equals(tempResult)) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) { e1.printStackTrace(); }
}
tempResult = result;
returnS.add(result);
Log.d("DEBUG", "loaded one of many user pages " + link.attr("href"));
}
And I capture the HTML code to be saved with a custom JavaScriptInterface
and a command called from the onPageFinished
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String u) {
result = "";
//view.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
if(u.equals("http://powerschool.lovejoyisd.net/public/home.html")
&& k == 0) {
view.loadUrl("javascript:document.getElementById('fieldAccount').value
=
'"+user+"';document.getElementsByName('pw')[0].value='"+password+"';document.getElementById('btn-enter').click();");
Log.d("DEBUG", "tried login at "+u);
k = 1;
}
Log.d("DEBUG", u);
view.loadUrl("javascript:window.HTMLOUT.processHTML(document.documentElement.innerHTML);");
}
}
So the result just spews all pages out instantly, without actually loading
each one. Making me assume the wv.loadUrl() in my for loop is stopping and
killing the previous page load attempt.
No comments:
Post a Comment