我在WebView中加载了一个Url。这是一个登录页面。我希望在我在此登录页面上输入凭据后获得身份验证后得到的响应。
这是我到目前为止所做的: -
Login.java
wvLogin = (WebView)findViewById(R.id.WVLogin);
// WebView settings
wvLogin.getSettings().setJavaScriptEnabled(true);
wvLogin.getSettings().setUseWideViewPort(true);
// Load url
wvLogin.loadUrl("myUrl");
// Set WebViewClient to load the url in webview and not phone's default browser.
wvLogin.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
redirected =view.getUrl();
redirected= Uri.decode(redirected);
if (redirected != null && redirected.equals(redirectUri)) {
wvLogin.stopLoading();
Intent mIntent = new Intent(LoginActivity.this, Update.class);
startActivity(mIntent);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
我该怎么做呢?
答案 0 :(得分:1)
使您的登录结果页面包含一个元标记,其中包含您需要的标记信息。
在您的活动设置中,您的wvLogin:
_wvLogin.getSettings().setJavaScriptEnabled(true);
_wvLogin.addJavascriptInterface(new Object() {
@SuppressWarnings("unused")
@android.webkit.JavascriptInterface
public void saveMetaTag(final String name, final String value) {
//Save meta tag value in some dictionary
}
@SuppressWarnings("unused")
@android.webkit.JavascriptInterface
public void onNoMoreMetaTags() {
//Process meta tags
}
}, "metaLoader");
然后执行javascript:
读取元标记wvLogin.loadUrl("javascript:var metaTags = document.getElementsByTagName('meta'); if (metaTags != null && metaTags.length > 0) {var i; for (i=0;i<metaTags.length;i++) { window.metaLoader.saveMetaTag(metaTags[i].name, metaTags[i].content); }; }; window.metaLoader.onNoMoreMetaTags();");
答案 1 :(得分:1)
试试这个 -
设置javascript界面
wvLogin.getSettings().setJavaScriptEnabled(true);
wvLogin.addJavascriptInterface(new JSInterface(), "HTMLOUT");
在onPageFinished()中获取HTML标记
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
添加定义您的JS接口
//JavaScript interface
class MyJavaScriptInterface {
public void showHTML(String result) {
String htmlString = result;
//Do your stuff with the result
}
}
这仅适用于API 17及更高版本
答案 2 :(得分:0)
为此,您需要使用WebViewClient。像 wvLogin.setWebChromeClient(new YourWebClient());
String htmlContent = "";
private class YourWebClient extends WebViewClient{
// you want to catch when an URL is going to be loaded
public boolean shouldOverrideUrlLoading (WebView view, String urlConection){
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
htmlContent = convertToString(is);
}
public String convertToString(InputStream inputStream){
StringBuffer string = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = reader.readLine()) != null) {
string.append(linea + "\n");
}
} catch (IOException e) {}
return string.toString();
}
}