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

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

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

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

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

Html表单代码:

Person:

Name Username

Title:

Department:

Groups to Search:

Students

Alumni

Staff

Faculty

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 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());

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐