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

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

更多相关文章

  1. Android利用Get/Post方式异步请求Json数据,显示在ListView中
  2. Android应用请求获取Root权限
  3. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析
  4. ListView监听OnItemClick无响应
  5. Android初级教程理论知识(第八章网络编程二)
  6. OkHttp的初步使用(get、post之{RequestBody、FormBody、Multipar
  7. 使用fiddler抓包手机请求数据
  8. Android(安卓)HttpClient 与JSON解析
  9. 利用Java反射技术调用Android中被隐藏的API

随机推荐

  1. 系出名门Android(10) - HTTP 通信, XML
  2. android笔记
  3. Android(安卓)Studio 使用AIDL
  4. Android上下文Context的那些小事
  5. Android中关于自定义Dialog的使用简介
  6. 转载:Android进程的内存管理分析
  7. Android不同版本获取当前wifi信息方法
  8. Android(安卓)关于休眠的几个坑点
  9. Android之Intent分析
  10. Android(安卓)Studio下jni应用