Android 浏览器支持“查看”javascript,例如以下代码可以启动浏览器应用程序以显示警报对话框:
String finalUrl = "javascript:alert('hello')";
Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(finalUrl));
startActivity(browserIntent);
通过 javascript 进行 post 操作的一个常见技巧是您通过 javascript 创建一个表单,然后提交它。所以理论上像下面这样的代码应该可以工作(部分代码是从这篇文章中复制的):
//String finalUrl = "http://localhost:7001/display/result.jsp?param=12345";
String finalUrl = "javascript:" +
"var to = 'http://localhost:7001/display/result.jsp';" +
"var p = {param:'12345',param2:'blablabla',param3:'whatever'};"+
"var myForm = document.createElement('form');" +
"myForm.method='post' ;" +
"myForm.action = to;" +
"for (var k in p) {" +
"var myInput = document.createElement('input') ;" +
"myInput.setAttribute('type', 'text');" +
"myInput.setAttribute('name', k) ;" +
"myInput.setAttribute('value', p[k]);" +
"myForm.appendChild(myInput) ;" +
"}" +
"document.body.appendChild(myForm) ;" +
"myForm.submit() ;" +
"document.body.removeChild(myForm) ;";
Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(finalUrl));
startActivity(browserIntent);