如何在Webview中自动输入用户名和密码

时间:2015-12-28 11:43:32

标签: android webview

我正在创建一个应用程序,以帮助我和其他学生快速登录我们学校的网站。学校网站是https://my.gediz.edu.tr/

我使用WebView创建了一个应用程序,我希望webview自动输入用户名和密码,以便用户在启动应用程序时已经登录。我用Google搜索并找到类似的东西,但它不起作用。

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView txtMsg;
WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android.support.v7.app.ActionBar actionBar=getSupportActionBar();

    // ActionBar actionBar=getActionBar();
    actionBar.setLogo(R.drawable.ic_actionbar_gediz1);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);

    txtMsg=(TextView)findViewById(R.id.textView1);
    //webview=(WebView)findViewById(R.id.webview1);
    demo1TrySpecificURL();

    //zoom
    webview.getSettings().setBuiltInZoomControls(true);
}

private void demo1TrySpecificURL(){
    webview=(WebView)findViewById(R.id.webview1);
    webview.getSettings().setJavaScriptEnabled(true);
    //webview.setWebViewClient(new MyWebClient(txtMsg, "https://my.gediz.edu.tr/"));
    webview.loadUrl("https://my.gediz.edu.tr/");

    webview.setWebViewClient(new WebViewClient(){

        public void onPageFinished(WebView view, String url){



            view.loadUrl("javascript:document.getElementsByName('username')[0].value = 'username'");
            view.loadUrl("javascript:document.getElementsByName('password')[0].value = 'password'");


            view.loadUrl("javascript:document.forms['Login'].submit()");
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //return super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.forward_page) {
        webview.goForward();
        return true;
    }

    else if (id == R.id.back_page) {
        webview.goBack();
        return true;
    }

    else if (id == R.id.reLoad_page) {
        webview.reload();
        return true;
    }

    else if (id == R.id.zoom_in) {
        webview.zoomIn();
         return true;
    }

    else if (id == R.id.zoom_out) {
        webview.zoomOut();
        return true;
    }



    return super.onOptionsItemSelected(item);
}

public void blaHandler(View view) {

    webview.setWebViewClient(new WebViewClient(){

        public void onPageFinished(WebView view, String url){

            view.loadUrl("javascript:document.getElementsByName('username')[0].value = 'username'");
            view.loadUrl("javascript:document.getElementsByName('password')[0].value = 'userpassword'");

            view.loadUrl("javascript:document.forms['Login '].submit()");
        }

    });

}
}

1 个答案:

答案 0 :(得分:1)

按照此处的建议尝试一些事情,

Android: insert data into html input

基本上,你必须编写javascript处理程序并将其挂钩到你的webview上。

相关问题