Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

java - An HtmlUnit alternative for android?

An alternative that allows me to fill an HTML form that has checkboxes and radiobuttons.

I was creating this android app that asks user input and sends that data to a website with an html form, fills it, submits the form, and returns the following results page.

I already managed to send data to the html form and retrieve the page using the HtmlUnit library in eclipse (I have posted the Java code for that below).

However, when I copied that code to my Android project I found out that Android does not support the HtmlUnit library.

Is there another alternative to HtmlUnit for Android? The alternative should be able to fill in texts, checkboxes, radiobuttons into an Html form and click on the submit button

Html form code:

<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 Code:

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());
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Guys I kind of found another method. Since I know the servers address I tried to post the data directly to it using a DataOutputStream. Look at the code below:

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;
}

As you can see I'm using the names of the Html Elements to access them through java. However as you can see from the html form code in original post the radio buttons have the same name, how can I access/fill them separately without using their names??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...