Android,如何将活动中的参数插入JavaScript

Android, How to insert parameter from activity into JavaScript?

本文关键字:参数 插入 JavaScript 活动中 Android      更新时间:2023-09-26

在我的应用程序中,我有一个活动作为登录表单。在获得用户id和密码后,我需要打开一个URL。

该网页包括登录表单(用户id和密码字段),当用户点击提交按钮时,通过java脚本函数处理的表单和表单内容将提交到服务器。

然而,我需要绕过显示这个页面,而是加载URL,将用户id和密码放入参数中,并代表用户调用JavaScript的doSubmit()函数,最终获得服务器响应。

我有用户id和密码,但我不知道如何将其插入JavaScript。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_loginweb);
        Log.i(TAG, "Try to create activity...");
        WebView webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl(LOGINPAGE_URL);

        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    } 
}
public class JavaScriptInterface {
        Context mContext;
        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }
        /** Show a toast from the web page */
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

任何建议都将不胜感激。感谢

在您的Javascript接口上添加一些方法,如下所示:

public String getUserId() {
        //return userId value here
    }
public String getPassword() {
        //return password value here
    }

来自javascript端:

//Startup stuff
$(document).ready(function(e) { 
    var userId = Android.getUserId();
    var password = Android.getPassword();
  $("#idofuserIdField").val(userId);
$("#idofPasswordField").val(password);
//callSubmitFunctionHere
});

我认为一个可能的解决方案是使用HttpClient(apache commons)并直接从您的活动中联系服务器:

public static void login(String pUsername, String pPassword){
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(YOUR_LOGIN_URL_WHICH_IS_CALLED_FROM_JS);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false));
method.setQueryString(new NameValuePair[] {      new NameValuePair("username", pUsername),new NameValuePair("password", pPassword)  });  
try {
  // Execute the method.
  int statusCode = client.executeMethod(method);
  if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: " + method.getStatusLine());
  }
  // Read the response body.
  byte[] responseBody = method.getResponseBody();
  // Deal with the response.
  // Use caution: ensure correct character encoding and is not binary data
  System.out.println(new String(responseBody));
} catch (HttpException e) {
  System.err.println("Fatal protocol violation: " + e.getMessage());
  e.printStackTrace();
} catch (IOException e) {
  System.err.println("Fatal transport error: " + e.getMessage());
  e.printStackTrace();
} finally {
  // Release the connection.
  method.releaseConnection();
}}

好吧,我认为这比这要多得多(使用SSL、Postparams和处理结果),但你会发现很多非常有用的教程。