我正在尝试实现后端服务器和android设备之间的数据传输,这是从设备到服务器的信息请求,服务器响应(使用UDP,因为它更快并且不需要TCP的功能,我猜...)
我从哪里开始实施这个?我查看了 DatagramSocket/Packet/SocketImplFactory 以及 InetAddress 和 SocketAddress 的 Android 文档。
这是正确的方向吗?现在我正在使用 HTTP Post 请求,是否有任何代码可回收?
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);