Android网络编程分为两种:基于http协议的,和基于socket的。
基于Http协议:HttpClient、HttpURLConnection、AsyncHttpClient框架等
基于Socket:
(1)针对TCP/IP的Socket、ServerSocket
(2)针对UDP/IP的DatagramSocket、DatagramPackage
(3)Apache Mina框架
一、HttpURLConnection的实现方式
String response = null; Url url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 新建连接实例 connection.setConnectTimeout(20000);// 设置连接超时时间,单位毫秒 //connection.setReadTimeout(20000);// 设置读取数据超时时间,单位毫秒 connection.setDoInput(true);// 是否打开输入流 true|false connection.setRequestMethod("POST");// 提交方法POST|GET //connection.setUseCaches(false);// 是否缓存true|false //connection.setRequestProperty("accept", "*/*"); //connection.setRequestProperty("Connection", "Keep-Alive"); //connection.setRequestProperty("Charset", "UTF-8"); //connection.setRequestProperty("Content-Length", String.valueOf(data.length)); //connection.setRequestProperty("Content-Type", "application/x-workAddress);
以上就是Android中网络通信几种方式的全部内容,希望对大家的学习有所帮助。