当前位置:操作系统 > 安卓/Android >>

Android 网络通讯、通信

网络操作是进行网络通信的安卓程序必不可少的一个重要部分,Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口)、Org.apache HttpComponents接口和Android.net.*(Android网络接口)。当然还可以使用浏览器webkit来进行网络访问等。其中,前两个接口可以用来进行http、socket通讯,后一个接口主要是用来判断安卓设备网络连接状况的。所以,本节重点说一下前两个接口。
 
   1、java.net.* HttpURLConnection接口
 
          此接口提供与联网有关的类,包括流和数据包套接字、Internet协议、常见HTTP处理。如:创建URL以及URLConnection/HttpURLConnection对象、设置连接参数、连接服务器、向服务器写数据、从服务器读取数据等通信。下例为常见java.net包的Http例子: 
 
   \
 
          HttpURLConnection是继承于URLConnection类,二者都是抽象类。其对象主要通过URL的openConnection方法获得。创建方法如下代码所示:
 
 
[html] 
URL url = new URL("访问的url");    
HttpURLConnection conn=(HttpURLConnection)url.openConnection();   
 
URL url = new URL("访问的url");  
HttpURLConnection conn=(HttpURLConnection)url.openConnection();           通过以下方法可以对请求的属性进行一些设置,如下所示:
 
 
[html] 
//设置输入和输出流    
conn.setDoOutput(true);    
conn.setDoInput(true);    
//设置请求方式为POST    
conn.setRequestMethod("POST");    
//POST请求不能使用缓存    
urlConn.setUseCaches(false);    
conn.connect();建立链接  
//关闭连接    
conn.disConnection();    
 
//设置输入和输出流  
conn.setDoOutput(true);  
conn.setDoInput(true);  
//设置请求方式为POST  
conn.setRequestMethod("POST");  
//POST请求不能使用缓存  
urlConn.setUseCaches(false);  
conn.connect();建立链接
//关闭连接  
conn.disConnection();             HttpURLConnection默认使用GET方式,例如下面代码所示:
 
 
[html]
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //使用HttpURLConnection打开连接                    
.getResponseCode();//得到连接状态  
if(nRC == HttpURLConnection.HTTP_OK){  
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());//得到读取的内容(流)      
// 为输出创建BufferedReader    
BufferedReader buffer = new BufferedReader(in);    
String inputLine = null;    
//使用循环来读取获得的数据    
while (((inputLine = buffer.readLine()) != null))    
{    
    //我们在每一行后面加上一个"\n"来换行    
    resultData += inputLine + "\n";    
}             
//关闭InputStreamReader    
}  
  
in.close();    
//关闭http连接    
conn.disconnect();   
 
               
                HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //使用HttpURLConnection打开连接                  
int nRC = http.getResponseCode();//得到连接状态
                if(nRC == HttpURLConnection.HTTP_OK){
                InputStreamReader in = new InputStreamReader(urlConn.getInputStream());//得到读取的内容(流)    
                // 为输出创建BufferedReader  
                BufferedReader buffer = new BufferedReader(in);  
                String inputLine = null;  
                //使用循环来读取获得的数据  
                while (((inputLine = buffer.readLine()) != null))  
                {  
                    //我们在每一行后面加上一个"\n"来换行  
                    resultData += inputLine + "\n";  
                }           
                //关闭InputStreamReader  
                }
             
                in.close();  
                //关闭http连接  
                conn.disconnect(); 
       如果需要使用POST方式,则需要setRequestMethod设置。代码如下:   
[html]
String httpUrl = "访问的url";    
//获得的数据    
String resultData = "";    
URL url = null;    
try   
{    
    //构造一个URL对象    
    url = new URL(httpUrl);     
}    
catch (MalformedURLException e)    
{    
    Log.e(DEBUG_TAG, "MalformedURLException");    
}    
if (url != null)    
{    
    try   
    {    
        // 使用HttpURLConnection打开连接    
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();    
        //因为这个是post请求,设立需要设置为true    
        conn.setDoOutput(true);    
        conn.setDoInput(true);    
        // 设置以POST方式    
        conn.setRequestMethod("POST");    
        // Post 请求不能使用缓存    
        conn.setUseCaches(false);    
        conn.setInstanceFollowRedirects(true);    
        // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的    
        conn.setReque
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,