
允许我填写具有复选框和单选按钮的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());}解决方法:
伙计们我找到了另一种方法.由于我知道服务器地址,因此我尝试使用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表单代码中看到的单选按钮具有相同的名称,如何在不使用其名称的情况下单独访问/填充它们?
总结以上是内存溢出为你收集整理的java – Android的HtmlUnit替代品?全部内容,希望文章能够帮你解决java – Android的HtmlUnit替代品?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)