Android的HtmlUnit替代方案?

时间:2016-07-06 09:38:53

标签: java android html selenium 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());
}

1 个答案:

答案 0 :(得分:1)

伙计我找到了另一种方法。由于我知道服务器地址,因此我尝试使用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表单代码中看到,单选按钮具有相同的名称,如何在不使用其名称的情况下单独访问/填充它们?