zoukankan      html  css  js  c++  java
  • android发送网络链接的几个类

    利用URLConncetion,HttpURLConnetion,HttpClient都可以发送网络的get,post请求,三者封装的程度由低到高,低级类能够实现的功能,高级类也能够实现

    例如;发送网络请求下载apk的时候:

    利用HttpURLConnetion,自己的代码如下

    public void downLoadApkByConnection(String urlString, ProgressDialog pd) {
            BufferedInputStream bis = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setConnectTimeout(5000);
                pd.setMax(connection.getContentLength());
            
                bis = new BufferedInputStream(connection.getInputStream());
                File file = new File(Environment.getExternalStorageDirectory()
                        + "/update/");
                if (!file.exists()) {
                    file.mkdirs();
                }
                fos=new FileOutputStream(file.getAbsoluteFile()+"绿萝空气.apk");
                bos=new BufferedOutputStream(fos);
                byte[] buffer = new byte[1024];
                int len = 0;
                int total = 0;
                while ((len = bis.read(buffer)) != -1) {
                    total += len;
                    bos.write(buffer);
                    pd.setProgress(total);
                    if (connection.getContentLength() == total) {
                        pd.dismiss();
                    }
                }
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try{
                bis.close();
                fos.close();
                bos.close();}
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

    如果利用httpclient

        public void downLoadApkByHttpClient(String urlString, ProgressDialog pd) {
            HttpGet get = new HttpGet(urlString);
            BufferedInputStream bis = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            //如果要设置响应超时事件,需要借助httpparams类了
             BasicHttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
               // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
                HttpClient client = new DefaultHttpClient(httpParams);
             
            try {
                
                HttpResponse response = client.execute(get);
                if (response != null
                        && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    //如果返回的文本内容,可以直接                
                    //String result=EntityUtils.toString(response.getEntity());
                //如果请求返回的是图片,二进制文件,需要得到相应流的话
                pd.setMax((int)response.getEntity().getContentLength());
                    bis=new BufferedInputStream(response.getEntity().getContent());
                  File file = new File(Environment.getExternalStorageDirectory()
                            + "/update2/");
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    fos=new FileOutputStream(file.getAbsoluteFile()+"绿萝空气.apk");
                    bos=new BufferedOutputStream(fos);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    int total = 0;
                    while ((len = bis.read(buffer)) != -1) {
                        total += len;
                        bos.write(buffer);
                        pd.setProgress(total);
                        if(total==response.getEntity().getContentLength()){
                            pd.dismiss();
                        }
                        
                         
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    关键是有些方法可能不清楚,如,如何得到相应的字符串,如何得到响应流?

  • 相关阅读:
    3D Computer Grapihcs Using OpenGL
    转:认识MyBean
    转:MyBean的安装
    转:MyBean简介
    Delphi常用关键字用法详解
    红鱼儿
    uniGUI-shuiying
    转:RTC搭建android下三层应用程序访问服务器MsSql-客户端
    转:RTC搭建android下三层应用程序访问服务器MsSql-服务器端
    转(Delphi 新窑洞):使用delphi 开发多层应用(十七)使用RTC web 服务器返回JSON
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3073817.html
Copyright © 2011-2022 走看看