Android的三种网络通信方式

本文详细介绍了一个基于Android平台的标准Java接口实现的Socket编程案例。通过一个客户端和服务端的完整示例,展示了如何创建Socket连接、进行数据收发及处理异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口)、Org.apache接口和Android.net.*(Android网络接口)。下面分别介绍这些接口的功能和作用。
1.标准Java接口
java.net.*提供与联网有关的类,包括流、数据包套接字(socket)、Internet协议、常见Http处理等。比如:创建URL,以及URLConnection/HttpURLConnection对象、设置链接参数、链接到服务器、向服务器写数据、从服务器读取数据等通信。这些在Java网络编程中均有涉及,我们看一个简单的socket编程,实现服务器回发客户端信息。

服务端:

  1. public class Server implements Runnable{  
  2.     @Override  
  3.     public void run() {  
  4.         Socket socket = null;  
  5.         try {  
  6.             ServerSocket server = new ServerSocket(18888);  
  7.             //循环监听客户端链接请求  
  8.             while(true){  
  9.                 System.out.println("start...");  
  10.                 //接收请求  
  11.                 socket = server.accept();  
  12.                 System.out.println("accept...");  
  13.                 //接收客户端消息  
  14.                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  15.                 String message = in.readLine();  
  16.                 //发送消息,向客户端  
  17.                 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);  
  18.                 out.println("Server:" + message);  
  19.                 //关闭流  
  20.                 in.close();  
  21.                 out.close();  
  22.             }  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }finally{  
  26.             if (null != socket){  
  27.                 try {  
  28.                     socket.close();  
  29.                 } catch (IOException e) {  
  30.                     e.printStackTrace();  
  31.                 }  
  32.             }  
  33.         }  
  34.           
  35.     }  
  36.     //启动服务器  
  37.     public static void main(String[] args){  
  38.         Thread server = new Thread(new Server());  
  39.         server.start();  
  40.     }  
  41. }  
客户端,MainActivity
[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     private EditText editText;  
  3.     private Button button;  
  4.     /** Called when the activity is first created. */  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.           
  10.         editText = (EditText)findViewById(R.id.editText1);  
  11.         button = (Button)findViewById(R.id.button1);  
  12.           
  13.         button.setOnClickListener(new OnClickListener() {  
  14.             @Override  
  15.             public void onClick(View v) {  
  16.                 Socket socket = null;  
  17.                 String message = editText.getText().toString()+ "\r\n" ;  
  18.                 try {  
  19.                     //创建客户端socket,注意:不能用localhost或127.0.0.1,Android模拟器把自己作为localhost  
  20.                     socket = new Socket("<span style="font-weight: bold;">10.0.2.2</span>",18888);  
  21.                     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter  
  22.                             (socket.getOutputStream())),true);  
  23.                     //发送数据  
  24.                     out.println(message);  
  25.                       
  26.                     //接收数据  
  27.                     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
  28.                     String msg = in.readLine();  
  29.                     if (null != msg){  
  30.                         editText.setText(msg);  
  31.                         System.out.println(msg);  
  32.                     }  
  33.                     else{  
  34.                         editText.setText("data error");  
  35.                     }  
  36.                     out.close();  
  37.                     in.close();  
  38.                 } catch (UnknownHostException e) {  
  39.                     e.printStackTrace();  
  40.                 } catch (IOException e) {  
  41.                     e.printStackTrace();  
  42.                 }  
  43.                 finally{  
  44.                     try {  
  45.                         if (null != socket){  
  46.                             socket.close();  
  47.                         }  
  48.                     } catch (IOException e) {  
  49.                         e.printStackTrace();  
  50.                     }  
  51.                 }  
  52.             }  
  53.         });  
  54.     }  
  55. }  
布局文件:
[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text="@string/hello" />  
  7.     <EditText android:layout_width="match_parent" android:id="@+id/editText1"  
  8.         android:layout_height="wrap_content"  
  9.         android:hint="input the message and click the send button"  
  10.         ></EditText>  
  11.     <Button android:text="send" android:id="@+id/button1"  
  12.         android:layout_width="fill_parent" android:layout_height=""wrap_content"></Button>  
  13. </LinearLayout>  
启动服务器:
  1. javac com/test/socket/Server.java  
  2. java com.test.socket.Server  
运行客户端程序:

结果如图:

注意:服务器与客户端无法链接的可能原因有:
没有加访问网络的权限:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
IP地址要使用:10.0.2.2
模拟器不能配置代理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值