I'm trying to make an application for the field data collection with the android phone app. Form. I made a form in android. Now I'm confused how to send the Datas to the server. Can I use txt file to send the data? And if so how to send the txt file from application to the server?
Asked
Active
Viewed 180 times
0
-
Use web requests, not text files – Anirudh Ramanathan Jul 22 '12 at 15:14
2 Answers
0
I would use XML
or JSON
instead of .txt
and send the data via HttpPut
or HttpPost
to a RESTful Webserver. there are some tutorials available on the internet on how to build such a server (vogella for example, who uses Jersey).
When the user finished his input and hits the send
-Button, your app collects all the data from the form
, wraps it into an XML
or JSON
-object and puts it to the server.

sschrass
- 7,014
- 6
- 43
- 62
0
The simplest way to send form data to a server is to use HttpClient, and HttpPost.
Try something like this:
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.example.com/process");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("name1", "value");
data.add(new BasicNameValuePair("name2", "value");
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
// do something with the response
}
catch (ClientProtocolException e) {
// do something
}
catch (IOException e) {
// do something
}
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.

nEx.Software
- 6,782
- 1
- 26
- 34
-
I appreciate the answer. But in my case the data may be collected offline, so data need to be saved, until there is wifi, GPRS or cabled connection. Hope this is little challenging then u responded. – nabin poudel Jul 22 '12 at 20:19
-
You can use any number of ways to serialize the data for storage. If you like you can store it to a text file then, when you are ready to send it, read it back in and fill the data for the post. What format you store it is up to you (JSON, XML, CSV, tab-delimited, etc...) as long as you can serialize and deserialize it. – nEx.Software Jul 22 '12 at 20:26