当前位置:编程学习 > wap >>

Android 怎么向打开的网页中自动填充用户名和密码?

想做一个自动登录的程序,ANDROID客户端打开相应的网页后,在用户名和密码框中自动填充用户名和密码,然后自动登录?怎么做啊?可有示例,谢谢了 --------------------编程问答-------------------- webview --------------------编程问答-------------------- 可有示例代码啥的? --------------------编程问答-------------------- 直接提交 请求 塞上用户名和密码 --------------------编程问答-------------------- 通过webview直接调用javascript比较困难啊
我看很多例子都是要html端回调webview的方法 --------------------编程问答--------------------
我写了一个demo,但是关键的部分不知道怎么实现阿
下面是代码
服务器端用struts2做了一个简单的登陆画面
想通过webview直接调用FillContent方法,这个方法
会填入用户名,密码,然后等待3秒之后自动登陆。

<%@page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
//function FillContent(UserName,PassWord){
function FillContent(){
//document.getElementsByName("username").item(0).value = UserName;
//document.getElementsByName("password").item(0).value = PassWord;
document.getElementsByName("username").item(0).value = '1';
document.getElementsByName("password").item(0).value = '2';
//睡眠n秒之后登录
window.setTimeout(Login,3000);
}

function Login(){
document.forms[0].submit();
}
</script>
</head>
<body">
<form action="Login.action" method="post">
<table align="center">
<caption>
<h3>用户登录</h3>
</caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密  码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重填"/>
</td>
</tr>
</table>
</form>
</body>
</html>


android的代码
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

<Button android:id="@+id/button1" 
android:layout_width="match_parent" 
android:text="Go" 
android:layout_height="wrap_content">
</Button>

</LinearLayout>



loginpagewebview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">
    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
    <WebView android:layout_width="fill_parent" android:id="@+id/webview1" android:layout_height="match_parent"></WebView>  
</LinearLayout>


两个activity:

package com.study;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AutoSetPageActivity extends Activity {
private Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
         Intent intent = new Intent();

Bundle bundle = new Bundle();

intent.putExtras(bundle);
intent.setClass(AutoSetPageActivity.this,LoginPageWebView.class);

startActivity(intent);
         }
        }
        );
    }
}



package com.study;

import android.app.Activity;
import android.webkit.WebViewClient;
import android.os.Bundle;
import android.webkit.WebView;
import org.apache.http.util.EncodingUtils;
import android.os.Handler;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;

public class LoginPageWebView extends Activity {
private WebView webview1;
private Handler mHandler = new Handler();  

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.loginpagewebview);

webview1 = (WebView)findViewById(R.id.webview1);

webview1.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
});

// JavaScriptを有効にする
webview1.getSettings().setJavaScriptEnabled(true);

//如果要直接在android里面提交用户名,密码,可以写在postData中
String postData = "";

String url="http://10.0.2.2:8080/LoginSample2";

//webview1.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
webview1.loadUrl(url);

/*webview1.addJavascriptInterface(new Object(){
public void clickOnAndroid(){
mHandler.post(new Runnable(){
public void run(){
webview1.loadUrl("javascript:FillContent('test','password')");
}
});
}
},"demo");*/
 String para1 = "charlie";

      String para2 = "24";

      String p1 = "{'username':\"" + para1 + "\"}";

      String p2 = "{'password':\" " + para2 + "\"}";

      //webview1.loadUrl("javascript:FillContent("+p1+","+p2 +")");
      webview1.loadUrl("javascript:FillContent()");
      Button mButton = (Button) findViewById(R.id.button1);     
                 //给button添加事件响应,执行JavaScript的fillContent()方法     
                 mButton.setOnClickListener(new OnClickListener() {     
                     public void onClick(View v) {     
                      webview1.loadUrl("javascript:FillContent()");                //这里的可以调用。。 
                     }     
                 }); 

//webview1.loadUrl("javascript:FillContent('test','password')");  

}catch(Exception e){
e.printStackTrace();
}
}
}



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.study"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AutoSetPageActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name="LoginPageWebView"></activity>
    </application>
    <uses-permission android:name ="android.permission.INTERNET" ></uses-permission>
</manifest>
--------------------编程问答-------------------- 楼主解决了没。我有同样的问题想请教你。 --------------------编程问答-------------------- 办法太多了啊  --------------------编程问答-------------------- 没有不能实现的 只有你想不到的 --------------------编程问答--------------------
引用 5 楼 zabaglione 的回复:
我写了一个demo,但是关键的部分不知道怎么实现阿
下面是代码
服务器端用struts2做了一个简单的登陆画面
想通过webview直接调用FillContent方法,这个方法
会填入用户名,密码,然后等待3秒之后自动登陆。

<%@page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
//function FillContent(UserName,PassWord){
function FillContent(){
//document.getElementsByName("username").item(0).value = UserName;
//document.getElementsByName("password").item(0).value = PassWord;
document.getElementsByName("username").item(0).value = '1';
document.getElementsByName("password").item(0).value = '2';
//睡眠n秒之后登录
window.setTimeout(Login,3000);
}

function Login(){
document.forms[0].submit();
}
</script>
</head>
<body">
<form action="Login.action" method="post">
<table align="center">
<caption>
<h3>用户登录</h3>
</caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr>
<td>密  码:<input type="text" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重填"/>
</td>
</tr>
</table>
</form>
</body>
</html>


android的代码
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

<Button android:id="@+id/button1" 
android:layout_width="match_parent" 
android:text="Go" 
android:layout_height="wrap_content">
</Button>

</LinearLayout>



loginpagewebview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">
    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
    <WebView android:layout_width="fill_parent" android:id="@+id/webview1" android:layout_height="match_parent"></WebView>  
</LinearLayout>


两个activity:

package com.study;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AutoSetPageActivity extends Activity {
private Button button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
         Intent intent = new Intent();

Bundle bundle = new Bundle();

intent.putExtras(bundle);
intent.setClass(AutoSetPageActivity.this,LoginPageWebView.class);

startActivity(intent);
         }
        }
        );
    }
}



package com.study;

import android.app.Activity;
import android.webkit.WebViewClient;
import android.os.Bundle;
import android.webkit.WebView;
import org.apache.http.util.EncodingUtils;
import android.os.Handler;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;

public class LoginPageWebView extends Activity {
private WebView webview1;
private Handler mHandler = new Handler();  

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.loginpagewebview);

webview1 = (WebView)findViewById(R.id.webview1);

webview1.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
});

// JavaScriptを有効にする
webview1.getSettings().setJavaScriptEnabled(true);

//如果要直接在android里面提交用户名,密码,可以写在postData中
String postData = "";

String url="http://10.0.2.2:8080/LoginSample2";

//webview1.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
webview1.loadUrl(url);

/*webview1.addJavascriptInterface(new Object(){
public void clickOnAndroid(){
mHandler.post(new Runnable(){
public void run(){
webview1.loadUrl("javascript:FillContent('test','password')");
}
});
}
},"demo");*/
 String para1 = "charlie";

      String para2 = "24";

      String p1 = "{'username':\"" + para1 + "\"}";

      String p2 = "{'password':\" " + para2 + "\"}";

      //webview1.loadUrl("javascript:FillContent("+p1+","+p2 +")");
      webview1.loadUrl("javascript:FillContent()");
      Button mButton = (Button) findViewById(R.id.button1);     
                 //给button添加事件响应,执行JavaScript的fillContent()方法     
                 mButton.setOnClickListener(new OnClickListener() {     
                     public void onClick(View v) {     
                      webview1.loadUrl("javascript:FillContent()");                //这里的可以调用。。 
                     }     
                 }); 

//webview1.loadUrl("javascript:FillContent('test','password')");  

}catch(Exception e){
e.printStackTrace();
}
}
}



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.study"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AutoSetPageActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name="LoginPageWebView"></activity>
    </application>
    <uses-permission android:name ="android.permission.INTERNET" ></uses-permission>
</manifest>

mark学习一下! --------------------编程问答--------------------
引用 8 楼 ydx1991 的回复:
没有不能实现的 只有你想不到的

具体点呢?
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,