首页 > 代码库 > Android的网络与通信(一)

Android的网络与通信(一)

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

public class Server implements Runnable{ 

@Override 

public void run() { 

        Socket socket = null; 

try { 

            ServerSocket server = new ServerSocket(18888); 

//循环监听客户端链接请求 

while(true){ 

System.out.println("start..."); 

//接收请求 

                socket = server.accept(); 

                System.out.println("accept..."); 

//接收客户端消息 

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

                String message = in.readLine(); 

//发送消息,向客户端 

                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); 

                out.println("Server:" + message); 

//关闭流 

                in.close(); 

                out.close(); 

            } 

        } catch (IOException e) { 

            e.printStackTrace(); 

        }finally{ 

if (null != socket){ 

try { 

                    socket.close(); 

                } catch (IOException e) { 

                    e.printStackTrace(); 

                } 

            } 

        } 

    } 

//启动服务器 

public static void main(String[] args){ 

        Thread server = new Thread(new Server()); 

        server.start(); 

    } 

客户端,MainActivity

public class MainActivity extends Activity { 

private EditText editText; 

private Button button; 

/** Called when the activity is first created. */ 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        editText = (EditText)findViewById(R.id.editText1); 

        button = (Button)findViewById(R.id.button1); 

        button.setOnClickListener(new OnClickListener() { 

@Override 

public void onClick(View v) { 

                Socket socket = null; 

                String message = editText.getText().toString()+ "\r\n" ; 

try { 

//创建客户端socket,注意:不能用localhost或127.0.0.1,Android模拟器把自己作为localhost 

                    socket = new Socket("<span style="font-weight: bold;">10.0.2.2</span>",18888); 

                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter 

                            (socket.getOutputStream())),true); 

//发送数据 

                    out.println(message); 

//接收数据 

                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

                    String msg = in.readLine(); 

if (null != msg){ 

                        editText.setText(msg); 

                        System.out.println(msg); 

                    } 

else{ 

                        editText.setText("data error"); 

                    } 

                    out.close(); 

                    in.close(); 

                } catch (UnknownHostException e) { 

                    e.printStackTrace(); 

                } catch (IOException e) { 

                    e.printStackTrace(); 

                } 

finally{ 

try { 

if (null != socket){ 

                            socket.close(); 

                        } 

                    } catch (IOException e) { 

                        e.printStackTrace(); 

                    } 

                } 

            } 

        }); 

    } 

Android的网络与通信(一)