• 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();
            }
        }

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

  • 相关阅读:
    编译安装php5 解决编译安装的php加载不了gd
    apache-php
    使用ab对网站进行压力测试
    正则表达式实例 -- 匹配Windows消息宏
    SDK 操作 list-view control 实例 -- 遍历进程
    malloc/free、new/delete 区别
    Python实例 -- 爬虫
    multimap实例 -- 添加、遍历数据
    CListCtrl 扩展风格设置方法---SetExtendedStyle和ModifyStyleEx
    创建指定大小的空文件
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3073817.html
Copyright © 2020-2023  润新知