Android网络编程

java.net.*(标准Java接口)提供与互联网有关的类。

 

Url url = "http://www.baidu.com";

HttpUrlConnection http = (HttpUrlConnection)url.openConnection();

int nRc = http.getResponseCode();  //获得链接状态

if(nRc == HttpURLConnection.HTTP_OK){

    InputStraem is = http.getInputStream();

}

 

Apache接口

    Android提供的Apache HttpClient,功能更加完善,为客户端的HTTP变成提供搞笑、最新、功能丰富的工具包支持。

    还提供了对它的一些封装和扩展,例如设置缺省的HTTP超时和缓存大小。

    功能主要包括创建HttpClient以及Get/Post、HttpRequest等对象,设置连接参数,执行HTTP操作,处理服务器返回结果等功能。

   

 

 1 //创建HttpClient,DefaultHttpClient表示默认属性
 2 
 3 HttpClient hc = new DefaultHttpClient();
 4 
 5 HttpGet get = new HttpGet(url);
 6 
 7 HttpResponse rp = hc.execute(get);
 8 
 9  
10 
11 if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
12 
13     InputStream is = rp.getEntity().getContent();

 

Android网络接口

    android.net.*包实际上是通过对Apache中HttpClient的封装来实现的一个HTTP编程接口。同事还提供了HTTP请求队列管理以及HTTP连接池管理,以提高并发请求情况下的处理效率,除此之外还有网络状态监视等接口、网络访问的Socket、常用的URL类以及有关Wifi相关的类等。

 

    Andrid中的Socket连接:

 1 //IP地址
 2 
 3 InetAddress inet = InetAddress.getByName("192.168.0.1");
 4 
 5  
 6 
 7 //端口
 8 
 9 Socket client = new Socket(inet,61020,true);
10 
11  
12 
13 //取得数据
14 
15 InputStream in = client.getInputStream();
16 
17 OutputStream out = client.getOutputStream();
18 
19  

 

Http通信

    Http(Hyper Text Transfer Protocol,超文本传输协议)用于传送WWW方式的数据。HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求,请求头包含了请求的方法URL、协议版本,以及包含请求修饰符、客户信息和内容的类似于MIME的消息结构。服务器以一个状态行作为响应,响应的内容包括消息协议的版本、成功或者错误编码,还包括服务器信息、实体元信息以及可能的实体内容。它是一个属于应用层的面向对象的协议,由于其简介、快速,适用于分布式超媒体信息系统。

    Android提供了HttpUrlConnection和HttpClient接口来开发HTTP程序。

 

HttpURLConnection是java的标准类,继承自URLConnection类,URLConnection与HTTPURLConnection都是抽象类,无法直接实例化对象。其对象主要通过URL的openConnection方法获得:

    

Url url = new URL(“http://www.baidu.com”);

HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();

 

openConnection方法只创建URLConnection或者HttpURLConnection实例,但是并不进行真正的连接操作。并且每次openConnection都将创建一个新的实例。

 

   

 //设置输入输出流

    connection.setDoOutput(true);

    connection.setDoInput(true);

   

    //设置传输方式:post\get

    connection.setRequestMethod("POST");

   

    //POst请求不能使用缓存

    connection.setUseCaches(false);

   

    //关闭连接

    connection.disconnect();

HttpURLConnection默认使用GET方式。

 

采用HttpUrlConnection的POST方式获取数据:

   

  1  //访问的URL地址
  2 
  3     String httpurl = "http://www.baidu.com/";
  4 
  5    
  6 
  7     //保存获取的数据
  8 
  9     URL url = null;
 10 
 11     try{
 12 
 13        url = new URL(httpurl);
 14 
 15     }catch{MalformedURLException e}{
 16 
 17        //错误信息
 18 
 19     }
 20 
 21    
 22 
 23     if(url != null){
 24 
 25        try{
 26 
 27            //使用HttpURLConnection打开连接
 28 
 29            HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
 30 
 31           
 32 
 33            //采用Post传递数据时输入、输出应该设置为true
 34 
 35            urlconn.setDoOutput(true);
 36 
 37            urlconn.setDoInput(true);
 38 
 39           
 40 
 41            //设置以post方式
 42 
 43            urlconn.setRequestMethod("POST");
 44 
 45           
 46 
 47            //post请求不能使用缓存
 48 
 49            urlconn.setUseCaches(false);
 50 
 51           
 52 
 53            //配置本次连接的Content-type为"application/x-www-form-urlencoded"
 54 
 55            urlconn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
 56 
 57           
 58 
 59            //以上所有的配置都需要在进行connect方法之前完成
 60 
 61            urlconn.connect();
 62 
 63           
 64 
 65            //DataOutputStream流
 66 
 67            DataOutputStream out = new DataOutputStream(urlconn.getOutputStream());
 68 
 69           
 70 
 71            //设置要上传的参数
 72 
 73            String content = "par="+URLEncoder.encode("ABCD","gb2312");
 74 
 75           
 76 
 77            //将要上传的内容写入到流中
 78 
 79            out.writeBytes(content);
 80 
 81           
 82 
 83            out.flush();
 84 
 85            out.close();
 86 
 87           
 88 
 89            String inputLine = null;
 90 
 91            //获取数据
 92 
 93            BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
 94 
 95           
 96 
 97            while(((inputLine = reader.readLine())!=null)){
 98 
 99               resultData += inputLine + "\n";
100 
101            }
102 
103           
104 
105            reader.close();
106 
107           
108 
109            urlconn.disconnect();
110 
111           
112 
113        }catch(IOException e){}
114 
115     }
原文地址:https://www.cnblogs.com/fanchangfa/p/2793326.html