我正在创建一个使用webview打开网站的android应用。我的目标是使用户不必再登录就可以访问该网站。我正在尝试使用通过用户名和密码从Web视图发送到网站的POST数据。
我要发布到的html部分是
<form action="" method="post" class="loginPad" autocomplete="off">
<h1 class="vPurple text-center loginText">
<i class="icon icon-lock vPink"></i>
Login
<span class="blink_text vPurple">>_</span>
</h1>
<div class="form-group">
<input type="text" name="username" value="" placeholder="Username" class="form-control rounded input-lg text-center no-border"
</div>
<div class="form-group">
<input type="password" name="password" value="" placeholder="Password" class="form-control rounded input-lg text-center no-border">
</div>
</form>
然后对于Android我有:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
final MainActivity activity = this;
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
});
String url = "http://www.*****.com/login.php";
String userName = "exampleUsername";
String password = "examplePassword";
String postData = "username="+userName+"& password="+password;
webView.postUrl(url, postData.getBytes());
使用我已有的资源,Webview会加载登录页面,但不会执行其他任何操作。