I have a web service that I am trying to authenticate in the background using webview. When I initially submit the request, it will work accordingly (failure / success based on credentials), but after it seems to me that I am getting a cached response.
Here is my webview setup code:
WebView browser = new WebView(this);
WebSettings settings = browser.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSavePassword(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
browser.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
Log.d("BROWSERPROGRESS", Integer.toString(progress));
}
});
jsInterface = new AddAccountJSInterface();
browser.addJavascriptInterface(jsInterface, "ADDACCOUNTJSINTERFACE");
browser.setWebViewClient(new AddAccountClient(this));
So, as you can see, I have two additional classes controlling my webView:
- An object that provides an interface for javascript (AddAccountJSInterface)
- Webviewclient
In addition, I have a WebChromeClient, but it is only available for debugging, and I'm sure it won’t hurt anything.
The JS interface simply provides an easy way to get the HTML body to do the analysis, so I'm sure this is not a problem either.
WebViewClient , "" -.
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(INSTALL_PREFIX)) {
HashMap<String, String> params = extractParameters(url);
verificationComplete(params);
return true;
}
return false;
}
@Override
public void onPageFinished(WebView view, String url){
if(invalidShop(view)) {
Toast.makeText(context, context.getString(R.string.no_find_shop), Toast.LENGTH_SHORT).show();
shopAddressField.requestFocus();
replaceUiElements(loadingBar, addAccountButton);
} else if(url.contains(ADMIN_AUTH_LOGIN)) {
if(invalidLogin(view)) {
Toast.makeText(context, context.getString(R.string.invalid_login),Toast.LENGTH_SHORT).show();
emailField.requestFocus();
replaceUiElements(loadingBar, addAccountButton);
} else {
String email = emailField.getText().toString();
String password = passwordField.getText().toString();
String submitJS = String.format(FORM_SUBMISSION_JS, email, password);
jsInterface.setInnerHTML("");
browser.loadUrl(submitJS);
}
}
}
3 , , , . (shopAddressField, usernameField, passwordField), javascript, ( -), .
, , , , (, cookie?) , , , .
:
JSInterface - Java, javascript -, . JSInterface , setInnerHtml (String html).
javascript, webview:
javascript:window.ADDACOUNTJSINTERFACE.setInnerHTML(document.body.innerHTML)
setInnerHtml:
public void setInnerHtml(String innerHtml) {
this.innerHtml = innerHtml;
}
, jsInterface.setInnerHtml(""), HTML-, ( , - ).
submitJS, Javascript, - :
String submitJS =
"javascript:(function() {
$('login-input').value='username';
$('password').value='password';
$('sign-in-form').up().submit();
})()"
webView.loadData(submitJS);