android 的 HtmlUnit 替代品?
An HtmlUnit alternative for android?
允许我填写具有复选框和单选按钮的 HTML 表单的替代方法。
我正在创建这个 android 应用程序,它要求用户输入并将该数据发送到带有 html 表单的网站,填写表单,提交表单,然后 returns 以下内容结果页面。
我已经设法将数据发送到 html 表单并使用 eclipse 中的 HtmlUnit 库检索页面(我已经发布了下面的 Java 代码)。
然而,当我将该代码复制到我的 Android 项目时,我发现 Android 不支持 HtmlUnit 库。
Android 的 HtmlUnit 是否有另一种选择?替代方案应该能够将文本、复选框、单选按钮填写到 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>
Html单位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());
}
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??
HtmlUnit 人员已开始致力于 android 支持。最大的问题 - HtmlUnit(使用 HttpClient)和部分集成在 android jdk 中的 HttpClient 版本之间的 class 版本冲突 - 现在已解决。
您可以使用 https://github.com/HtmlUnit/htmlunit-android 项目的发行版。
请尝试并报告任何问题。
允许我填写具有复选框和单选按钮的 HTML 表单的替代方法。
我正在创建这个 android 应用程序,它要求用户输入并将该数据发送到带有 html 表单的网站,填写表单,提交表单,然后 returns 以下内容结果页面。
我已经设法将数据发送到 html 表单并使用 eclipse 中的 HtmlUnit 库检索页面(我已经发布了下面的 Java 代码)。
然而,当我将该代码复制到我的 Android 项目时,我发现 Android 不支持 HtmlUnit 库。
Android 的 HtmlUnit 是否有另一种选择?替代方案应该能够将文本、复选框、单选按钮填写到 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>
Html单位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());
}
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??
HtmlUnit 人员已开始致力于 android 支持。最大的问题 - HtmlUnit(使用 HttpClient)和部分集成在 android jdk 中的 HttpClient 版本之间的 class 版本冲突 - 现在已解决。 您可以使用 https://github.com/HtmlUnit/htmlunit-android 项目的发行版。
请尝试并报告任何问题。