Android的HtmlUnit替代品?

另一种选择是允许我填写带有复选框和单选按钮的HTML表单。

我正在创建一个需要用户输入的Android应用程序,并将该数据使用html表单发送到网站,然后将其填写,提交表单并返回以下结果页面。

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

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

HtmlUnit是否有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表单代码中看到的那样,单选按钮具有相同的名称,如何在不使用其名称的情况下分别访问/填充它们?

以上是 Android的HtmlUnit替代品? 的全部内容, 来源链接: utcz.com/qa/420736.html

回到顶部