Android平臺(tái)有三種網(wǎng)絡(luò)接口可以使用,他們分別是:java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口)。下面分別介紹這些接口的功能和作用。
1.標(biāo)準(zhǔn)Java接口
java.net.*提供與聯(lián)網(wǎng)有關(guān)的類,包括流、數(shù)據(jù)包套接字(socket)、Internet協(xié)議、常見(jiàn)Http處理等。比如:創(chuàng)建URL,以及URLConnection/HttpURLConnection對(duì)象、設(shè)置鏈接參數(shù)、鏈接到服務(wù)器、向服務(wù)器寫(xiě)數(shù)據(jù)、從服務(wù)器讀取數(shù)據(jù)等通信。這些在Java網(wǎng)絡(luò)編程中均有涉及,我們看一個(gè)簡(jiǎn)單的socket編程,實(shí)現(xiàn)服務(wù)器回發(fā)客戶端信息。
下面用個(gè)例子來(lái)說(shuō)明:
A、客戶端:
新建Android項(xiàng)目工程:SocketForAndroid(這個(gè)隨意起名字了吧,我是以這個(gè)建立的!)
下面是main_activity.xml的代碼:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hint" /> <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/send" /></LinearLayout>
MainActivity.java的代碼入下:
package com.yaowen.socketforandroid;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;public class MainActivity extends AppCompatActivity { private EditText message; private Button send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化兩個(gè)UI控件 message = (EditText) findViewById(R.id.message); send = (Button) findViewById(R.id.send); //設(shè)置發(fā)送按鈕的點(diǎn)擊事件響應(yīng) send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Socket socket = null; //獲取message輸入框里的輸入的內(nèi)容 String msg = message.getText().toString() + "/r/n"; try { //這里必須是192.168.3.200,不可以是localhost或者127.0.0.1 socket = new Socket("192.168.3.200", 18888); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream() ) ), true); //發(fā)送消息 out.println(msg); //接收數(shù)據(jù) BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); //讀取接收的數(shù)據(jù) String msg_in = in.readLine(); if (null != msg_in) { message.setText(msg_in); System.out.println(msg_in); } else { message.setText("接收的數(shù)據(jù)有誤!"); } //關(guān)閉各種流 out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != socket) { //socket不為空時(shí),最后記得要把socket關(guān)閉 socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); }}最后別忘記添加訪問(wèn)網(wǎng)絡(luò)權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
B、服務(wù)端:
package service;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class ServerAndroid implements Runnable { @Override public void run() { Socket socket = null; try { ServerSocket server = new ServerSocket(18888); // 循環(huán)監(jiān)聽(tīng)客戶端鏈接請(qǐng)求 while (true) { System.out.println("start..."); // 接收請(qǐng)求 socket = server.accept(); System.out.println("accept..."); // 接收客戶端消息 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String message = in.readLine(); System.out.println(message); // 發(fā)送消息,向客戶端 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); out.println("Server:" + message); // 關(guān)閉流 in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != socket) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 啟動(dòng)服務(wù)器 public static void main(String[] args) { Thread server = new Thread(new ServerAndroid()); server.start(); }}C、啟動(dòng)服務(wù)器,控制臺(tái)會(huì)打印出“start...”字符串!
D、運(yùn)行Android項(xiàng)目文件,如下圖:

在輸入框里輸入如下字符串,點(diǎn)發(fā)送按鈕:

服務(wù)器收到客戶端發(fā)來(lái)的消息并打印到控制臺(tái):

2、Apache接口
對(duì)于大部分應(yīng)用程序而言JDK本身提供的網(wǎng)絡(luò)功能已遠(yuǎn)遠(yuǎn)不夠,這時(shí)就需要Android提供的Apache HttpClient了。它是一個(gè)開(kāi)源項(xiàng)目,功能更加完善,為客戶端的Http編程提供高效、最新、功能豐富的工具包支持。
下面我們以一個(gè)簡(jiǎn)單例子來(lái)看看如何使用HttpClient在Android客戶端訪問(wèn)Web。
首先,要在你的機(jī)器上搭建一個(gè)web應(yīng)用test,有兩個(gè)很簡(jiǎn)單的PHP文件:hello_get.php和hello_post.php!
內(nèi)容如下:
hello_get.php的代碼如下:
<html> <body> Welcome <?php echo $_GET["name"]; ?><br> You connected this page on : <?php echo $_GET["get"]; ?> </body></html>
hello_post.php的代碼如下:
<html><body>Welcome <?php echo $_POST["name"]; ?><br>You connected this page on : <?php echo $_POST["post"]; ?></body></html>
在原來(lái)的Android項(xiàng)目里新建一個(gè)Apache活動(dòng)類:Apache.java,代碼如下:
package com.yaowen.socketforandroid;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;/** * Created by YAOWEN on 2015/11/10. */public class ApacheActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView; private Button get1, post1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.apache); textView = (TextView) findViewById(R.id.textView); get1 = (Button) findViewById(R.id.get); post1 = (Button) findViewById(R.id.post); get1.setOnClickListener(this); post1.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.get) { //注意:此處ip不能用127.0.0.1或localhost,Android模擬器已將它自己作為了localhost String url = "http://192.168.3.200/test/hello_get.php?name=yaowen&get=GET"; textView.setText(get(url)); } if (v.getId() == R.id.post) { String url="http://192.168.3.200/test/hello_post.php"; textView.setText(post(url)); } } /** * 以post方式發(fā)送請(qǐng)求,訪問(wèn)web * * @param url web地址 * @return 響應(yīng)數(shù)據(jù) */ private String post(String url) { BufferedReader reader = null; StringBuffer sb = null; String result = ""; HttpClient client = new DefaultHttpClient(); HttpPost requset = new HttpPost(url); //保存要傳遞的參數(shù) List<NameValuePair> params = new ArrayList<NameValuePair>(); //添加參數(shù) params.add(new BasicNameValuePair("name", "yaowen")); params.add(new BasicNameValuePair("post","POST")); try { HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8"); requset.setEntity(entity); HttpResponse response = client.execute(requset); if (response.getStatusLine().getStatusCode() == 200) { System.out.println("post success"); reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); sb = new StringBuffer(); String line = ""; String NL = System.getProperty("line.separator"); while ((line = reader.readLine()) != null) { sb.append(line); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != reader) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != sb) { result = sb.toString(); } } return result; } /** * 以get方式發(fā)送請(qǐng)求,訪問(wèn)web * * @param url web地址 * @return 響應(yīng)數(shù)據(jù) */ private static String get(String url) { BufferedReader bufferedReader = null; StringBuffer sb = null; String result = ""; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); //發(fā)送請(qǐng)求,得到響應(yīng) try { HttpResponse response = client.execute(request); //請(qǐng)求成功 if (response.getStatusLine().getStatusCode() == 200) { bufferedReader = new BufferedReader( new InputStreamReader( response.getEntity() .getContent() ) ); sb = new StringBuffer(); String line = ""; String NL = System.getProperty("line.separator"); while ((line = bufferedReader.readLine()) != null) { sb.append(line); } } } catch (IOException e) { e.printStackTrace(); } finally { if (null != bufferedReader) { try { bufferedReader.close(); //bufferedReader=null; } catch (IOException e) { e.printStackTrace(); } } if (null != sb) { result = sb.toString(); } } return result; }}新建一個(gè)apache.XML文件,如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="通過(guò)按鈕選擇不同方式訪問(wèn)網(wǎng)頁(yè)" /> <Button android:id="@+id/get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get" /> <Button android:id="@+id/post" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="post" /></LinearLayout>
結(jié)果運(yùn)行如下:

3.android.net編程:
常常使用此包下的類進(jìn)行Android特有的網(wǎng)絡(luò)編程,如:訪問(wèn)WiFi,訪問(wèn)Android聯(lián)網(wǎng)信息,郵件等功能。
這里就不詳細(xì)做例子了,因?yàn)檫@個(gè)接觸比較多~~~。
下面給大家介紹Android的網(wǎng)絡(luò)通訊
我們知道,Java提供的Socket可以完成了兩臺(tái)PC機(jī)的通信。TCPServer需要客戶端和服務(wù)器,服務(wù)器用SocketServer和Socket完成,客戶端使用Socket完成......這些我們都很熟悉。此章主要是通過(guò)TCPServer完成Android 與PC機(jī)的通信,
首先來(lái)看我們熟悉的服務(wù)器程序:

上圖用主方法main其他一個(gè)Thread,然后在run方法里面無(wú)限監(jiān)聽(tīng)發(fā)送過(guò)來(lái)的內(nèi)容,如果有內(nèi)容則輸出。利用的是Java的API,很經(jīng)典。
接下來(lái)就是在Android端發(fā)送數(shù)據(jù)給PC端,其代碼如下:

這樣就完成了Android與PC機(jī)間的通信。
新聞熱點(diǎn)
疑難解答
圖片精選