I have an application built that works like a form, it takes four fields and validates the information in order to make sure that no invalid characters are entered. These four fields are stored in variables:
- Phone
- Name
Comments
Now I want to submit the form data (whatever is entered into these four fields and stored to the variables) to a url (will use http://www.test.com), but I am not sure how to go about doing this. I think I am looking for something called the HttpURLConnection, but I am not sure how to specify which variable is sent. The code below I found from the website 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(); }
}
}
Any help would be much appreciated, thank you!
//using xml request
Add the fields to the url as parameters.
Not tested, but it should work in a GET Http request.
The simplest way to send form data to a server is to use HttpClient, and HttpPost.
Try something like this:
Note, you'll want to perform this operation in an AsyncTask so you don't lock up the UI thread waiting for the networking operations to complete.
Edit
Here's a simple quick example of what it might look like in an AsyncTask.