我有一个像表单一样工作的应用程序,它需要四个字段并验证信息以确保没有输入无效字符。这四个字段存储在变量中:
- 电话
- 姓名
- 电子邮件
评论
现在我想将表单数据(在这四个字段中输入并存储到变量中的任何内容)提交到一个 url(将使用http://www.test.com),但我不知道该怎么做. 我想我正在寻找一种叫做 HttpURLConnection 的东西,但我不确定如何指定发送哪个变量。下面的代码是我从网站http://developer.android.com/reference/java/net/HttpURLConnection.html找到的
private class UploadFilesTask extends AsyncTask<URL, Integer, Long>{ protected Long doInBackground(URL... urls) { try { HttpClient http = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.test.com"); List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new BasicNameValuePair("phone", "value")); data.add(new BasicNameValuePair("name", "value")); data.add(new BasicNameValuePair("email", "value")); data.add(new BasicNameValuePair("comments", "value")); post.setEntity(new UrlEncodedFormEntity(data)); HttpResponse response = http.execute(post); // do something with the response } catch (ClientProtocolException e) { // do something finish(); } catch (IOException e) { // do something finish(); }
}
}
任何帮助将不胜感激,谢谢!