Android,如何将活动中的参数插入JavaScript
Android, How to insert parameter from activity into JavaScript?
在我的应用程序中,我有一个活动作为登录表单。在获得用户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和处理结果),但你会发现很多非常有用的教程。
相关文章:
- Android,如何将活动中的参数插入JavaScript
- 在Javascript代码中插入URL参数
- 在更改时选择,插入类似函数的参数或指令之间的差异
- 未捕获的类型错误:无法在“CSSStyleSheet”上执行“插入规则”:需要 2 个参数,但仅存在 1 个参数
- 如何将参数插入正则表达式
- Javascript:插入一个变量作为参数
- 将变量的值作为参数插入,并在对象的函数参数中插入其值
- 如何使用jQueryappend插入HTML部分包含onclick函数,该函数有一个参数
- 如何在提交GET表单后将变量插入到生成的url参数字符串中
- 在函数中插入输入值作为参数不起作用
- 空格-在纯javascript中插入参数,不带手柄样式的大括号
- 将数组或字符串转换为参数列表[SQLite插入绑定参数]
- 为什么Facebook不“点赞”呢?按钮在通过JavaScript插入时尊重其参数
- 我怎么能插入一个参数像forEach,减少和类似的
- 动态输入,插入方法名和参数
- 不能在Angular中插入url参数
- Meteor.js,Mongo:可以向用户插入新的文档或参数.来自客户端的配置文件
- Javascript在字符串模板中插入方法参数
- 正确插入参数
- 使用参数插入到 SQLite 数据库中