Android to execute web js

Android to execute web js

本文关键字:js web execute to Android      更新时间:2023-09-26

我正在编写一个可以自动登录网站的应用程序。有一个Javascript函数提交表单。我通过在Google Chrome控制台输入它来测试它,并且它工作了。有人能告诉我如何在Java中做到这一点吗?请帮助我。下面是表单HTML代码:

<form target="_blank" method="POST" id="login_form2" action="https://webcat.hkpl.gov.hk/auth/login?wicket:interface=:0:loginPanel:loginForm::IFormSubmitListener::&amp;theme=WEB&amp;locale=zh_TW">
    <h4 class="orange">我的帳戶</h4>
    <label style="display:none" for="acc-box2">號碼</label>
    <input title="號碼" data-hint="號碼" class="text-field" name="username" type="password" style="display: none;" id="acc-box2">
    <input title="號碼" value="號碼" type="text" class="text-field" style="width: 150px; margin-bottom: 5px; display: block;">
    <label class="orange show_pass" for="show-check2"><input title="顯示" id="show-check2" type="checkbox" name="c2">顯示</label>
    <label style="display:none" for="pass-box2">網上密碼</label>
    <input value="004484284548649879936:lcyfpggwj-i" name="cx" type="hidden">
    <input value="UTF-8" name="ie" type="hidden">
    <input style="display: none;" class="text-field" name="password" type="password" id="pass-box2">
    <input style="width:150px;margin-bottom:5px;" class="text-field" type="text" value="網上密碼" title="網上密碼">
    <input type="submit" title="提交" style="display:none;">
    <div style="clear:both;"></div>
    <a onclick="$('#login_form2').submit()" class="ac_login_btn" href="javascript:void(0)">
        <img height="32" width="94" title="登入" alt="登入" src="/common/tc/images/main_login_btn.png">
    </a>
    <div style="       vertical-align: top;       line-height: 1;       font-size: small;       padding-left: 100px;       color: #c63006;      ">請緊記在關閉瀏覽器視窗前,先要登出你的帳戶。</div>
</form>

提交函数是$('#login_form2').submit()

在java中你可以试试这个,如果你想从应用程序登录到网站

public static void loginToSite(HttpClient client, String username, String password){
List<NameValuePair> attributes = new ArrayList<NameValuePair>();
attributes .add(new BasicNameValuePair("email", username));
attributes .add(new BasicNameValuePair("pword", password));
attributes .add(new BasicNameValuePair("action", "modifyPALS"));
attributes .add(new BasicNameValuePair("Submit", "Login"));
HttpPost httpPost = new HttpPost();
try{
    httpPost .setURI(new URI("https://webcat.hkpl.gov.hk/auth/login"));
    httpPost .setEntity(new UrlEncodedFormEntity(attributes ));
    client.execute(httpPost );
    AppStatus s = getSiteConnectionStatus(client, site);
}
catch (URISyntaxException e){
     e.printStackTrace();
} 
catch (ClientProtocolException e) {
    e.printStackTrace();
} 
catch (IOException e) {
    e.printStackTrace();
}

}

但我不确定这是不是你想要的

如果你想把整个项目转换成java,你必须自己做。你不应该指望任何人为你做这件事。

我可以提个建议,这样你就不用把所有东西都转换成java了。你可以使用Webview并在那个Webview中加载html文件。在这种情况下,您不必在java中重写表单。