Android HttpConnection 使用

来源:互联网 发布:淘宝在线店招制作 编辑:程序博客网 时间:2024/05/17 17:44

1,从Intent获取网页,发送请求,将网页以流的形式读回来。

1)创建一个URL对象:URL url = new URL("http://www.iteye.com");

2) 获取链接 :HttpURLConnection conn=(HttpURLConnection )url.openConnection();

3) 设置超时时间:conn.setConnectTimeout(6*1000);

4) 设置允许输入输出:conn.setDoInput(true);conn.setDoOutput(true);

5) 设置请求模式:conn.setRequestMethod("POST");//注意,要大写

6)设置读取超时:conn.setReadTimeout(true);

7)得到网络返回的输入输出流:InputStream/OutputStream ios = conn.getInputStream()/getOutputStream();

8)判断是否连接成功:conn.getResponseCode()!=200  throw new RuntimeException("请求url失败");

注意:

--在对大文件的操作时,要将文件写到SDCard上面,不要直接写到手机内存上.
--操作大文件是,要一遍从网络上读,一遍要往SDCard上面写,减少手机内存的使用.这点很重要,面试经常会被问到.
--对文件流操作完,要记得及时关闭.

Java代码  收藏代码
  1. public class HttpConnect {  
  2.     private boolean isCancel = false;  
  3.     public void setCancel(boolean isCancel) {  
  4.         this.isCancel = isCancel;  
  5.     }  
  6.     public byte[] open(String strUrl, String postData) {  
  7.         byte[] data = null;  
  8.         InputStream is = null;  
  9.         OutputStream os = null;  
  10.         ByteArrayOutputStream baos = null;  
  11.         HttpURLConnection conn = null;  
  12.         try {// 为了测试取消连接  
  13.                 // Thread.sleep(5000);  
  14.                 // http联网可以用httpClient或java.net.url  
  15.             URL url = new URL(strUrl);  
  16.             conn = (HttpURLConnection) url.openConnection();  
  17.             conn.setDoInput(true);  
  18.             conn.setDoOutput(true);  
  19.             conn.setConnectTimeout(1000 * 30);  
  20.             conn.setReadTimeout(1000 * 30);  
  21.             if (Tools.isNull(postData)) {  
  22.                 conn.setRequestMethod("GET");  
  23.             } else {  
  24.                 conn.setRequestMethod("POST");  
  25.                 os = conn.getOutputStream();  
  26.                 byte[] sendData = postData.getBytes();  
  27.                 os.write(sendData);// 将post数据发出去  
  28.             }  
  29.             if (isCancel) {  
  30.                 LogUtil.i("open""已经取消掉了连接");  
  31.                 return null;  
  32.             }  
  33.             int responseCode = conn.getResponseCode();  
  34.             if (responseCode == 200) {  
  35.                 is = conn.getInputStream();  
  36.                 baos = new ByteArrayOutputStream();  
  37.                 byte[] buffer = new byte[1024 * 8];  
  38.                 int size = 0;  
  39.                 while ((size = is.read(buffer)) >= 1) {  
  40.                     baos.write(buffer, 0, size);  
  41.                 }  
  42.                 data = baos.toByteArray();  
  43.             }  
  44.   
  45.         } catch (Exception e) {  
  46.             ExceptionUtil.handle(e);  
  47.         } finally {  
  48.             try {  
  49.                 if (is != null) {is.close();}  
  50.                 if (os != null) {os.close();}  
  51.                 if (baos != null) {baos.close();}  
  52.                 conn = null;  
  53.             } catch (IOException e) {  
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.         if (isCancel) {  
  58.             LogUtil.i("open""已经取消掉了连接");  
  59.             return null;  
  60.         }  
  61.         return data;  
  62.     }  
  63.   
  64. }  
  65. 参考链接:
1 0