Android网络传输框架的核心代码量对比

1. HttpURLConnection

int nStartPos = 0;
int timeout = 15000;
int NUMLIMIT_FAIL=5;
long nEndPos = 30000000;
byte[] b = new byte[blockSize];  
URL url;
String linkString="http://XXXXXX";
try {
   url = new URL(linkString);
   HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
   httpConnection.setConnectTimeout(timeout);
   httpConnection.setReadTimeout(timeout);
   httpConnection.setRequestMethod(isDownload==1?"GET":"POST");
   String sProperty = "bytes="  + nStartPos +  "-" +nEndPos;   //sProperty设置的是开始下载的位置,即断点。若nStartPos为0则完整下载整个文件
   httpConnection.setRequestProperty("RANGE" , sProperty);//当然也可以这么写:httpConnection.setRequestProperty("RANGE","bytes=50-20070");
   InputStream input = httpConnection.getInputStream();
   FileOutputStream f = new FileOutputStream(new File(sPath));//读取网络文件,写入指定的文件中
   int nRead,failedCnt=1;
   while (nStartPos < nEndPos&&failedCnt<=NUMLIMIT_FAIL) {
      try {
         nRead = input.read(b, 0, blockSize);
         if(nRead>0){
            f.write(b, 0, nRead);
            nStartPos += nRead;
         }
         else {
            break;
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   httpConnection.disconnect();
   f.close();
} catch (Exception e1) {
   e1.printStackTrace();
}

2. OKhttp

OkHttpClient client = new OkHttpClient();
String url = "http://XXXXXXXX";
try {
       Request request = new Request.Builder().url(url).build();
       Response response = client.newCall(request).execute();
       InputStream is = response.body().byteStream();
       Bitmap bm = BitmapFactory.decodeStream(is);
       imageView.setImageBitmap(bm);
} catch (IOException e) {
            e.printStackTrace();
}

3. Volley

String url = "http://XXXXXXXXXX";
try {
    ImageRequest imgrequest = new ImageRequest(url,new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bitmap) {
                            imageView.setImageBitmap(bitmap);
                        }}, 0, 0, Bitmap.Config.RGB_565,
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                            System.out.println(error.getLocalizedMessage());
                        }
                    });
            RequestQueue queue = Volley.newRequestQueue(this);
            queue.add(imgrequest);
        }catch (Exception e){
            e.printStackTrace();
        }

4. TransFramework

String url = "http://XXXXXXXXXXXXXXX";
ImageRequest imgrequest = new ImageRequest(url,new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        imageView.setImageBitmap(bitmap);
                    }}, 0, 0, null,new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        System.out.println(error.getLocalizedMessage());
                    }
                });
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(imgrequest);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值