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

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

  • 相关阅读:
    转:IPhone之ASIFormDataRequest POST操作架构设计/ 处理网络超时问题
    LLDB和GDB比较
    为线程设置一个名字 [mythread setName:@"第一个子线程"];
    杀死一个线程
    ios 开发框架原始雏形 01
    iOS开发:设置应用程序图标和引导画面
    一个奇怪的现象 在GDB模式和LLDB 模式下 同样代码不同反应 AudioServicesCreateSystemSoundID
    iOS中GCD的魔力
    提升app 应用程序运行速度的几个常用方法
    IOS开发缓存机制之—本地缓存机制
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3073817.html
Copyright © 2011-2022 走看看