Reputation: 259
I am trying to implement data transfer between a back-end server and the android device which is a request from device to server for information and the server responds(UDP use because it is faster and the functionality of TCP isn't needed, I guess...)
Where do I begin for implementing this? I have looked at the Android Docs for DatagramSocket/Packet/SocketImplFactory as well as InetAddress and SocketAddress.
Is this the right direction? Right now I am using HTTP Post requests, is any of that code recyclable?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
if (fleetID != null) {
nameValuePairs.add(new BasicNameValuePair("fleetID", fleetID));
}
nameValuePairs.add(new BasicNameValuePair("user", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
//TODO: response failure? need to ensure success
HttpEntity resEntity = response.getEntity();
String strEntity = EntityUtils.toString(resEntity);
//Log.i("RESPONSE", strEntity);
return (strEntity);
Upvotes: 1
Views: 2434
Reputation: 232
The server and Android components can both be written in Java using the the standard Java DatagramSocket class. A quick example can be found here (in this instance it is sending a String from the command prompt: http://systembash.com/content/a-simple-java-udp-server-and-udp-client/. Basically, you make a DatagramSocket instance with a given port number and you send/recieve the data as a byte[]. However, I would be careful with using UDP. TCP ensures that no data is lost when the file is sent, and therefore ensures that data is not corrupted as it is sent.
Upvotes: 2