Android網(wǎng)絡編程Socket
在Android的網(wǎng)絡通訊中,通常會使用Socket進行設備間數(shù)的數(shù)據(jù)通訊,使用Http來對網(wǎng)絡數(shù)據(jù)進行請求。
1、Socket(套接字)
不管是有過Java開發(fā)經(jīng)驗還是.NET開發(fā)經(jīng)驗的同學都應該對Socket有或多或少的了解,常見的TCP或者UDP協(xié)議其實都是基于Socket來實現(xiàn)的。
Socket是用于描述網(wǎng)絡上的一個設備中的一個進程或者應用程序的,Socket由IP地址和端口號兩部分組成。IP地址用來定位設備,端口號用來定位 應用程序或者進程,比如我們常見的運行在80端口上的HTTP協(xié)議。Socket的常見格式為:192.168.1.1:1234。
那么應用程序是如何通過Socket來與網(wǎng)絡中的其他設備進行通訊的呢?通常情況下,Socket通信有兩部分,一部分為監(jiān)聽的Server端,一部分為 主動請求連接的Client端。Server端會一直監(jiān)聽Socket中的端口直到有請求為止,當Client端對該端口進行連接請求時,Server端 就給予應答并返回一個Socket對象,以后在Server端與Client端的數(shù)據(jù)交換就可以使用這個Socket來進行操作了。
2、Android中使用Socket進行數(shù)據(jù)交換
ServerSocket
建立服務端(Server)時,需要使用ServerSocket對象,這個對象會自動對其構造函數(shù)中傳入的端口號進行監(jiān)聽,并在收到連接請求后,使 用ServerSocket.accept()方法返回一個連接的的Socket對象。這個方法并不需要我們像在.NET中那樣使用Start方法,它會 自動進行監(jiān)聽的。
Socket
不管建立客戶端(Client)還是在進行其他數(shù)據(jù)交換方面的操作時,都需要使用Socket類。Socket類在進行初始化時需要出入Server 端的IP地址和端口號,并返回連接到Server端的一個Socket對象,如果是連接失敗,那么將返回異常。同ServerSocket,也是自動進行 連接請求的。
通過上面兩個步驟后,Server端和Client端就可以連接起來了,但是僅僅連接起來是沒有任何作用的,數(shù)據(jù)交換才是我們的目的,這時候就需要用到IO流中的OutputStream類和InputStream類。
OutputStream——可寫流
當應用程序需要對流進行數(shù)據(jù)寫操作時,可以使用Socket.getOutputStream()方法返回的數(shù)據(jù)流進行操作。
InputStream——可讀流
當應用程序要從流中取出數(shù)據(jù)時,可以使用Socket.getInputStream()方法返回的數(shù)據(jù)流進行操作。
看看完整的代碼吧
- View Code
- package LiB.Demo;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class SocketHelper {
- private static ServerSocket serverSocket = null;
- private static Socket client = null;
- private final static int port = 9048;
- private static BufferedReader br= null;
- private static BufferedWriter bw = null;
- /**
- * 創(chuàng)建一個SocketServer對象用來建立服務器
- * @throws IOException
- */
- public static void CreateServer() throws IOException
- {
- serverSocket = new ServerSocket(port,10);
- System.out.println("start listening...");
- }
- /**
- * 創(chuàng)建一個Socket對象用來連接SocketServer對象
- * @param dstName Server對象的ip地址
- * @return
- * @throws IOException
- */
- public static Socket CreateClient(String dstName) throws IOException
- {
- Socket socket = new Socket(dstName, port);
- //Socket sockets = new Socket("192.168.8.12",port);
- return socket;
- }
- /**
- * 返回一個已經(jīng)連接到服務器上的Socket對象
- * @throws IOException
- */
- public static void GetClinetSocket() throws IOException
- {
- client = serverSocket.accept();
- System.out.println("get a connected client");
- }
- /**
- * 向socket對象所獲取的流中發(fā)送數(shù)據(jù)
- * @param socket
- * @param msg
- * @throws IOException
- */
- public static void SendMsg(Socket socket , String msg) throws IOException
- {
- bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- bw.write(msg);
- bw.flush();
- bw.close();
- }
- /**
- * 獲取socket對象流中數(shù)據(jù)
- * @param socket
- * @param msg
- * @return
- * @throws IOException
- */
- public static String ReceiveMsg(Socket socket, String msg) throws IOException
- {
- br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String receiveMsg = "Receive msg:"+ br.readLine();
- br.close();
- return receiveMsg;
- }
- /**
- * 釋放socket對象
- * @throws IOException
- */
- public static void Close() throws IOException
- {
- if(client != null)
- {
- client.close();
- }
- if(serverSocket != null)
- {
- serverSocket.close();
- }
- }
- }
下頁將為您介紹HTTP通訊的內(nèi)容
#p#
3、HTTP通訊
在開始前先簡單介紹下HTTP協(xié)議中的兩種不同的請求方式——GET和POST。GET方式在進行數(shù)據(jù)請求時,會把數(shù)據(jù)附加到URL后面?zhèn)鬟f給服務 器,比如常見的:http://XXX.XXX.XXX/XX.aspx?id=1,POST方式則是將請求的數(shù)據(jù)放到HTTP請求頭中,作為請求頭的一 部分傳入服務器。所以,在進行HTTP編程前,首先要明確究竟使用的哪種方式進行數(shù)據(jù)請求的。
在Android中,可以有兩種方式可以用來進行Http編程:1、HttpURLConnection;2、HttpClient。
HttpURLConnection
HttpURLConnection是繼承自URLConnection的一個抽象類,在HTTP編程時,來自HttpURLConnection的類是所有操作的基礎,獲取該對象的代碼如下:
- View Code
- public HttpURLConnection urlconn= null;
- private void Init() throws IOException
- {
- if (urlStr=="")
- {
- urlStr="http://www.baidu.com";
- }
- URL url = new URL(urlStr);
- //打開一個URL所指向的Connection對象
- urlconn = (HttpURLConnection)url.openConnection();
- }View Code
- public HttpURLConnection urlconn= null;
- private void Init() throws IOException
- {
- if (urlStr=="")
- {
- urlStr="http://www.baidu.com";
- }
- URL url = new URL(urlStr);
- //打開一個URL所指向的Connection對象
- urlconn = (HttpURLConnection)url.openConnection();
- }
HttpURLConnection對網(wǎng)絡資源的請求在默認情況下是使用GET方式的,所以當使用GET方式時,不需要我們做太多的工作:
- View Code
- public HttpURLConnection urlconn= null;
- private void Init() throws IOException
- {
- if (urlStr=="")
- {
- urlStr="http://www.baidu.com";
- }
- URL url = new URL(urlStr);
- //打開一個URL所指向的Connection對象
- urlconn = (HttpURLConnection)url.openConnection();
- }
- /**
- * Http中的get請求,在Url中帶有請求的參數(shù),請求的URL格式通常為:"http://XXX.XXXX.com/xx.aspx?param=value"
- * 在android中默認的http請求為get方式
- * @return
- * @throws IOException
- */
- public String HttpGetMethod() throws IOException
- {
- if(urlconn == null)
- {
- Init();
- }
- String result = StreamDeal(urlconn.getInputStream());
- urlconn.disconnect();
- return result;
- }
當我們需要使用POST方式時,就需要使用setRequestMethod()來設置請求方式了。
- View Code
- /**
- * Http中的post請求,不在Url中附加任何參數(shù),這些參數(shù)都會通過cookie或者session等其他方式以鍵值對的形式key=value傳送到服務器上,完成一次請求
- * 請求的URL格式通常為:"http://XXX.XXXX.com/xx.aspx"
- * @param param 請求的鍵名
- * @param value 請求的數(shù)據(jù)值
- * @throws IOException
- */
- public String HttpPostMethod(String key,String value) throws IOException
- {
- if (urlconn==null)
- {
- Init();
- }
- //設置該URLConnection可讀
- urlconn.setDoInput(true);
- //設置該URLConnection可寫
- urlconn.setDoOutput(true);
- //使用POST方式來提交數(shù)據(jù)
- urlconn.setRequestMethod("POST");
- //不運行緩存
- urlconn.setUseCaches(false);
- //當使用POST方式進行數(shù)據(jù)請求時,我們可以手動執(zhí)行connect動作,當然,這個動作其實在getOutputStream()方法中會默認執(zhí)行的
- //上面那些設置URLConnection屬性的動作,一定要在connect動作執(zhí)行前,因為一旦動作已經(jīng)執(zhí)行,熟悉設置就沒有任何作用了
- urlconn.connect();
- //使用POST方式時,我們需要自己構造部分Http請求的內(nèi)容,因此我們需要使用OutputStream來進行數(shù)據(jù)寫如操作
- OutputStreamWriter writer = new OutputStreamWriter(urlconn.getOutputStream());
- String urlQueryStr = key+"="+URLEncoder.encode(value, "Utf-8");
- writer.write(urlQueryStr);
- writer.flush();
- writer.close();
- //獲取返回的內(nèi)容
- String result = StreamDeal(urlconn.getInputStream());
- return result;
- }
HttpClient
這個類并不是來自Android的,而是來自org.apache.http。和HttpURLConnection相同,HttpClient也存在GET和POST兩種方式。
HttpGet
在HttpClient中,我們可以非常輕松使用HttpGet對象來通過GET方式進行數(shù)據(jù)請求操作,當獲得HttpGet對象后我們可以 使用HttpClient的execute方法來向我們的服務器發(fā)送請求。在發(fā)送的GET請求被服務器相應后,會返回一個HttpResponse響應對 象,利用這個響應的對象我們能夠獲得響應回來的狀態(tài)碼,如:200、400、401等等。
- View Code
- public String HttpGetMethod()
- {
- String result = "";
- try
- {
- HttpGet httpRequest = new HttpGet(urlStr);
- HttpClient httpClient = new DefaultHttpClient();
- HttpResponse httpResponse = httpClient.execute(httpRequest);
- if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK)
- {
- result = EntityUtils.toString(httpResponse.getEntity());
- }
- else
- {
- result = "null";
- }
- return result;
- }
- catch(Exception e)
- {
- return null;
- }
- }
HttpPost
當我們使用POST方式時,我們可以使用HttpPost類來進行操作。當獲取了HttpPost對象后,我們就需要向這個請求體傳入鍵值 對,這個鍵值對我們可以使用NameValuePair對象來進行構造,然后再使用HttpRequest對象最終構造我們的請求體,***使用 HttpClient的execute方法來發(fā)送我們的請求,并在得到響應后返回一個HttpResponse對象。其他操作和我們在HttpGet對象 中的操作一樣。
- View Code
- public String HttpPostMethod(String key,String value)
- {
- String result = "";
- try
- {
- // HttpPost連接對象
- HttpPost httpRequest = new HttpPost(urlStr);
- // 使用NameValuePair來保存要傳遞的Post參數(shù)
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- // 添加要傳遞的參數(shù)
- params.add(new BasicNameValuePair(key, value));
- // 設置字符集
- HttpEntity httpentity = new UrlEncodedFormEntity(params, "Utf-8");
- // 請求httpRequest
- httpRequest.setEntity(httpentity);
- // 取得默認的HttpClient
- HttpClient httpclient = new DefaultHttpClient();
- // 取得HttpResponse
- HttpResponse httpResponse = httpclient.execute(httpRequest);
- // HttpStatus.SC_OK表示連接成功
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- // 取得返回的字符串
- result = EntityUtils.toString(httpResponse.getEntity());
- return result;
- } else {
- return "null";
- }
- }
- catch(Exception e)
- {
- return null;
- }
- }
三、總結
可以說Android如果不進行與網(wǎng)絡資源進行交互的話,它就和我們當初的普通系統(tǒng)沒有任何區(qū)別了,所以網(wǎng)絡編程對Android開發(fā)來說有非常特殊的意義。