在android中以编程方式提交html表单

Man*_*esh 5 android android-emulator

我想在android中以编程方式提交表单.我不希望任何用户与Web浏览器进行交互.用户将在EditField中提供输入,然后输入将通过HTTP post方法通过HTTPwebmethod提交.但我没有取得任何成功.请指教.我在java中使用过HTMLUnit但它在android中不起作用.

  final WebClient webClient = new WebClient();
 final HtmlPage page1 = webClient.getPage("http://www.mail.example.com");
     final HtmlForm form = page1.getHtmlElementById("loginform");

    final HtmlSubmitInput button = form.getInputByName("btrn");
    final HtmlTextInput textField1 = form.getElementById("user");
   final HtmlPasswordInput textField2 =          form.getElementById("password");textField1.setValueAttribute("user.name");
    textField2.setValueAttribute("pass.word"); final HtmlPage page2 = button.click();
Run Code Online (Sandbox Code Playgroud)

all*_*aws 11

哎呀.抱歉.看起来你正试图通过浏览器发布POST.

这是我在Android中用来完成HTTP POST的代码片段,无需通过网络浏览器:

HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost(url);  
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));  
nameValuePairs.add(new BasicNameValuePair("name2", "value2")); 
nameValuePairs.add(new BasicNameValuePair("name3", "value3"));   
// etc...
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpClient.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)

我认为这应该适用于你想要做的事情.我将TIMEOUT_MS设置为10000(所以,10秒)

然后你可以使用以下内容读出服务器的响应:

BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
Run Code Online (Sandbox Code Playgroud)