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

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

  • 相关阅读:
    archlinux .bash_history
    Ubuntu环境下挂载新硬盘
    软碟通 UltraISO U启替代品 Win32DiskImager 无设备 无盘符 无u盘 无优盘 解决方案 之diskpart
    delphi Integer overflow
    MSBuild Tools offline
    delphi synedit免费的拼写检查器dll
    git 自定义命令行
    lua编译
    gcc ar
    Windows Subsystem for Linux (WSL)挂载移动硬盘U盘 卸载 c d 盘
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3073817.html
Copyright © 2011-2022 走看看