微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Android的HtmlUnit替代品?

允许我填写具有复选框和单选按钮的HTML表单的替代方法.

我正在创建这个Android应用程序,询问用户输入并将该数据发送到具有html表单的网站,填写它,提交表单,并返回以下结果页面.

我已经设法将数据发送到html表单并使用eclipse中的HtmlUnit库检索页面(我已经发布了下面的Java代码).

但是,当我将该代码复制到我的Android项目时,我发现Android不支持HtmlUnit库.

还有其他替代HtmlUnit for Android?替代方案应该能够将文本,复选框,单选按钮填入Html表单并单击提交按钮

Html表单代码

<form method="post" action="https://www.xxxxx.com/cgi-bin/xxxxxx.cgi">


<p><em>Person:</em>
<input size="18" name="name"><br>
<input type="radio" name="login" value="no" checked="">Name <input     type="radio" name="login" value="yes">Username</p>

<p><em>Title:</em>
<input size="18" name="title"></p>

<p><em>Department:</em>
<input size="18" name="department"></p>

<p><em>Groups to Search:</em><br>
<input type="checkBox" name="get_student" value="yes" checked=""> Students<br>
<input type="checkBox" name="get_alum" value="yes" checked=""> Alumni<br>

<input type="checkBox" name="get_staff" value="yes" checked=""> Staff<br>
<input type="checkBox" name="get_faculty" value="yes" checked=""> Faculty</p>

<p><input type="submit" value="Search"></p>
</form>

HtmlUnit Java代码

public static String submittingForm() throws Exception {


    final WebClient webClient = new WebClient(browserVersion.FIREFOX_38);
    webClient.getoptions().setJavaScriptEnabled(false);
    webClient.getoptions().setThrowExceptionOnScriptError(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 

    WebRequest request = new WebRequest(new URL("https://www.xxxxx.com/"));

    // Get the first page
    HtmlPage page1 = webClient.getPage(request);

    System.out.println("PULLING LINKS/ LOADING:");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    List<HtmlForm> listform = page1.getForms();
    HtmlForm form = listform.get(0);


    HtmlElement Name = page1.getElementByName("name");
    Name.click();
    Name.type("Adonay");
    HtmlElement nameRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='no']");
    HtmlElement userRadio = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='radio' and @value='yes']");
   /* userRadio.click(); click when username wanted*/
    HtmlElement Title = page1.getElementByName("title");
    Title.click();
    Title.type("");
    HtmlElement Department = page1.getElementByName("department");
    Department.click();
    Department.type("");
    HtmlElement studentBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkBox' and @name='get_student']");
    studentBox.click();
    //add clicker here
    HtmlElement alumniBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkBox' and @name='get_alum']");
    alumniBox.click();
    //add clicker here
    HtmlElement staffBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkBox' and @name='get_staff']");
    staffBox.click();
    //add clicker here
    HtmlElement facultyBox = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='checkBox' and @name='get_faculty']");
    facultyBox.click();
    //add clicker here



    HtmlElement button = page1.getFirstByXPath("/html/body//div[@id='wrapper']//div[@id='layout']//div[@id='container']//div[@id='col1']//div[@id='content']//div[@class='directory-search']//form//input[@type='submit' and @value='Search']");
    // Change the value of the text field

    // Now submit the form by clicking the button and get back the second page.
    HtmlPage page2 = button.click();
    webClient.waitForBackgroundJavaScript(200);
    return(page2.asXml());
}

解决方法:

伙计们我找到了另一种方法.由于我知道服务器地址,因此我尝试使用DataOutputStream将数据直接发布到它.看下面的代码

public String searchDirectory() throws IOException {
    URL url = new URL("https://www.xxxxx.com/xxxxx/xxxxx.cgi");
    URLConnection con = url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream printout = new DataOutputStream (con.getoutputStream ());

    String parameters =("name=" + URLEncoder.encode("DATA HERE", "UTF-8"));
    printout.writeBytes (parameters);
    printout.flush ();
    printout.close ();
        /*InputStream inputStream = getApplicationContext().getResources().getAssets().open("White Pages Query Results.html");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        */
    DataInputStream input = new DataInputStream (con.getInputStream ());
    String line;
    String htmlCode = "";
    while((line = input.readLine()) != null) {
        htmlCode += line;
    }
    return htmlCode;
}

正如您所看到的,我正在使用Html Elements的名称通过java访问它们.但是正如您可以从原始帖子中的html表单代码中看到的单选按钮具有相同的名称,如何在不使用其名称的情况下单独访问/填充它们?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐