Java – Webview’s method of automatically entering passwords

Webview’s method of automatically entering passwords… here is a solution to the problem.

Webview’s method of automatically entering passwords

I’m new to Java and Android and I have a question.
I think the solution might be very simple, but I can’t fix it.

I created an app with a WebView where I want the webview to automatically enter the username and password so that the user is already logged in when they start the app.
I googled and found something similar, but it doesn’t work

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("https://euterpe.webuntis.com/WebUntis/index.do#main");

webView.setWebViewClient(new WebViewClient ());

class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {

handler.proceed("Username", "password");

}
}

Solution

Hello, please try this :

webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl("javascript:document.getElementsByName('school').value = 'schoolname'");
webView.loadUrl("javascript:document.getElementsByName('j_username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('j_password').value = 'password'");

webView.loadUrl("javascript:document.forms['login'].submit()");

The first line enables JavaScript

All other lines fill out the form and eventually submit the form.

Please look at the string apostrophes, I edited my answer.

Related Problems and Solutions