Android有两套http的API,刚开始使用网络编程时多少有些迷惑到底用哪个好呢?其实孰优孰劣无需再争论,google已经指出HttpUrlConnection是Android更优的选择,并在SDK文档中引用了博客(需要代理访问)http://android-developers.blogspot.com/2011/09/androids-http-clients.html来阐述各自的优缺点。国内也有些博客大致翻译了上面的内容,并对了一些测试,可惜测试不严密,某博客甚至得出HttpUrlConnection的下载速度快几倍的结论,其实并没有公平反映出二者的下载速度。
虽然我已经使用过HttpUrlConnection实现了一个轻量级http引擎用于下载,但是好奇心还是促使我写了一个测试程序用于比较二者的性能。由于HttpClient仅仅是一个接口,所以我选用了其实现类DefaultHttpClient和HttpUrlConnection做比较,方法很简单,分别下载同一个文件10次,然后计算耗时的平均值,测试代码片段如下:
@Override public void onClick(View v) { if (v.equals(mTestHttpClientBtn)) { new Thread(new Runnable() { @Override public void run() { long averageTime = 0; for (int i = 0; i < 10; i++) { File file = new File(getFilesDir(), String.valueOf(i) + ".file"); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); } catch (FileNotFoundException e) { Log.e(TAG, "", e); return; } long startTime = System.currentTimeMillis(); testHttpClient(fileOutputStream); long stopTime = System.currentTimeMillis(); averageTime += stopTime - startTime; } averageTime /= 10; // 测试完成 Message msg = new Message(); msg.what = MSG_TEST_HTTP_CLIENT_DONE; msg.obj = averageTime; mHandler.sendMessage(msg); } }).start(); return; } if (v.equals(mTestHttpUrlConnectionBtn)) { new Thread(new Runnable() { @Override public void run() { long averageTime = 0; for (int i = 0; i < 10; i++) { File file = new File(getFilesDir(), String.valueOf(i + 10) + ".file"); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); } catch (FileNotFoundException e) { Log.e(TAG, "", e); return; } long startTime = System.currentTimeMillis(); testHttpUrlConnection(fileOutputStream); long stopTime = System.currentTimeMillis(); averageTime += stopTime - startTime; } averageTime /= 10; // 测试完成 // 返回栏目页:http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/ Message msg = new Message(); msg.what = MSG_TEST_HTTP_URL_CONNECTION_DONE; msg.obj = averageTime; mHandler.sendMessage(msg); } }).start(); return; } } private void testHttpClient(FileOutputStream fileOutputStream) { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpEntity entity = null; InputStream inputStream = null; try { HttpGet httpGet = new HttpGet(TEST_URL); HttpResponse httpResponse = httpClient.execute(httpGet); StatusLine statusLine = httpResponse.getStatusLine(); if (statusLine == null) { throw new Exception("no status line !!!"); } int responseCode = statusLine.getStatusCode(); if (responseCode < 200 || responseCode >= 300) { throw new Exception("response error !!!"); } entity = httpResponse.getEntity(); if (entity == null) { throw new Exception("no entity !!!"); } inputStream = entity.getContent(); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesRead); } } catch (Exception e) { Log.e(TAG, "", e); } finally { try { if (inputStream != null) { inputStream.close(); } if (entity != null) { entity.consumeContent(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (Exception e) { Log.e(TAG, "", e); } } } private void testHttpUrlConnection(FileOutputStream fileOutputStream) { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; try { httpURLConnection = (HttpURLConnection) new URL(TEST_URL).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); int responseCode = httpURLConnection.getResponseCode(); if (responseCode < 200 || responseCode >= 300) { throw new Exception("response error !!!"); } inputStream = httpURLConnection.getInputStream(); int bytesRead = -1; byte[] buffer = new byte[4096]; while ((bytesRead = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, bytesRead); } } catch (Exception e) { Log.e(TAG, "", e); } finally { try { if (inputStream != null) { inputStream.close(); } if (httpURLConnection != null) { httpURLConnection.disconnect(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (Exception e) { Log.e(TAG, "", e); } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索exception
, new
, httpurlconnection
, null
, inputstream
, fileoutputstream
, inputStream.read()
HttpURLConnection类
peerconnectionclient、httpclientconnection、httpurlconnection、urlconnection、urlconnection post,以便于您获取更多的相关知识。