Android网络---使用标准的Java接口--java-net

43 阅读2分钟

#IP地址

  • IP地址是给每个连接在Internet上的主机分配的一个32bit地址java.net中处理IP地址的类是InetAddress 其结构如图:

  • InetAddress的具体用法例程:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IntelAddressTest {

   public String GetHostAddress (String strHostName)
    {
        InetAddress address = null;
        try
        {
            address = InetAddress.getByName (strHostName);
        }    catch(UnknownHostException e)
        {
            System.out.println(e.getMessage());
        }
        return address.getHostAddress();
    }


    void GetAllIP (String strHostName)
    {
        InetAddress[] addr = null;
        try
        {
            addr = InetAddress.getAllByName (strHostName);

            for(int i=0;i<addr.length;i++)
                System.out.println(addr[i]);
        }
        catch(UnknownHostException e)
        {
            System.out.println(e.getMessage());
        }
    }

}

  • 在写网络编程方面的代码时,必须注意异常的捕获, 网络异常是比较正常的现象,

  • 比如说当前网络繁忙网络连接超时更是“家常便饭”。 因此在写网络编程的代码时,必须养成捕获异常的好习惯

  • 查看完函数说明后,必须要注意网络异常的说明。 特别注意,在使用getByAddress ()函数的时候 必须捕获UnknownHostException这个异常。

  • 其中InetAddress.toString()隐含调用println调用
    自动输出主机名IP 地址

#URL地址

  • Java中直接提供了类URL 来处理和URL相关的知识:
  • 用法例程:
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest {

    void EasyURL (String strURL) throws MalformedURLException {
        URL url = new URL(strURL);
        try
        {
            InputStream html = url.openStream ();
            int c;
            do{
                c= html.read();
                if(c!=-1) System.out.println((char)c);
            }while(c!=-1);
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

#套接字Socket类 套接字Socket类的基本结构如图:

  • 套接字通信的基本思想: 客户端建立一个到服务器的链接, 一旦连接建立了, 客户端就可以往套接字里写入数据,并向服务器发送数据; 反过来, 服务器读取客户端写入套接字里的数据。

  • 用法例程:

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class SocketTest {

    void WebPing (String strURL)
    {
        try
        {
            InetAddress addr;
            Socket sock = new Socket(strURL,80);
            addr = sock.getInetAddress ();

            System.out.println("Connceted to" + addr);
            
            sock.close();
        }
        catch(IOException e)
        {
            System.out.println(e.getMessage());
        }
    }
}
  • 另外还有其他套接字, 例如DatagramSocket(通过UDP通信的套接字)、 MulticastSocket (一种用于多点传送的套接字)以及 ServerSocket(一种用于监听来自客户端的连接的套接字)等

#URLConncetion类

  • 一般情况下,URL类就可以满足我们的项目需求, 但是在一些特殊情况下,如HTTP数据头的传递, 这个时候我们就得使用URLConncetion
  • 例程:
    void SendRequest (String strURL) throws IOException {
        URL url = new URL(strURL);
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection)url.openConnection ();
        } catch (IOException e) {
            e.printStackTrace();
        }

        conn.setDoInput (true);
        conn.setDoOutput (true);
        conn.setRequestProperty ("Content-type","application/xxx");
        conn.connect ();

        System.out.println(conn.getResponseMessage ());
        InputStream is = conn.getInputStream();
        int c;
        do{
            c = is.read();
            if(c!=-1) System.out.println((char)c);
        }while(c!=-1);
    }

#在Android中使用java.net

  • 在文件AndroidManifest.xml中添加“android.permission.INTERNET”
<uses-permission android:name="android.permission.INTERNET"/>
  • 编写布局xml