![](/img/trans.png)
[英]how can a program login to a website having a html form for username and password
[英]can someone tell me how to copy username & password info from one intent to website and press the login button
这是我要使用我的应用程序https://campusvirtual.uclm.es/login/index.php访问的网站,但是,我不想直接这样做。 我创建了另一个布局,用户将使用该布局来填写他/她的用户名和密码。 问题是代码不起作用。 它是这样的:- 这是我要复制到网站的意图中的代码:
public class MainActivity extends Activity {
private EditText uSer;
private EditText paS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.cvlogin);
uSer = (EditText) findViewById(R.id.user);
paS = (EditText) findViewById(R.id.pass);
Button web = (Button) findViewById(R.id.button1);
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
load();
}
});
}
protected void load(){
Intent i = new Intent(MainActivity.this, CVLogIn.class );
//create a bundle
Bundle bundle = new Bundle();
bundle.putString("user_n",uSer.getText().toString());
bundle.putString("pass_n",paS.getText().toString());
// add the intent
i.putExtras(bundle);
startActivity(i);
}
}
-这是网站,这是我如何编写代码:
public class CVLogIn extends Activity {
private WebView myWebView;
private String uSer;
private String paS;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.seeweb);
myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Provide a WebViewClient for your WebView
myWebView.setWebViewClient(new MyWebViewClient());
// Capturamos el intent creado en MainActivity
Bundle bundle = this.getIntent().getExtras();
if (bundle != null){
myWebView.loadUrl("https://campusvirtual.uclm.es/login/index.php");
//Recogemos los valores
uSer = bundle.getString("user_n");
paS = bundle.getString("pass_n");
myWebView.loadUrl("javascript: {" +
"document.getElementById('username').value = '"+uSer +"';" +
"document.getElementById('password').value = '"+paS+"';" +
"var frms = document.getElementsById('loginbtn');" +
"frms[0].submit(); };");
}
// ProgressBar
progressBar = (ProgressBar) findViewById(R.id.progressbar);
myWebView.setWebChromeClient(new WebChromeClient()
{
@Override
public void onProgressChanged(WebView view, int progress)
{
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
progressBar.incrementProgressBy(progress);
if(progress == 100)
{
progressBar.setVisibility(View.GONE);
}
}
});
}
@Override
public void onBackPressed() {
// Check if there's history
if (this.myWebView.canGoBack())
this.myWebView.goBack();
else
super.onBackPressed();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Forward button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoForward()) {
myWebView.goForward();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("campusvirtual.uclm.es")) {
// This is my web site, so do not override; let my WebView load
// the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
您需要等到第一个loadUrl()
完成加载后再调用第二个loadUrl()
。 loadUrl()
是异步的,因此在您尝试运行JavaScript时不会加载网页。 尝试在loadUrl()
中执行第二个loadUrl()
onPageFinished()
,看看是否有帮助。
除此之外,使用JavaScript控制台(例如Firebug)在常规的Web浏览器中测试JavaScript,然后查看是否存在任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.