2013-10-08 43 views
3

我正在嘗試在我的Android應用程序中執行一個AsyncTask類,以分析下載和上傳的網絡連接速度。我現在正在下載部分,但我沒有得到我期望的結果。我正在一個Wifi網絡上進行測試,這個網絡的速度一直保持在15Mbps以下,但是從我的應用程序中得到的結果更差不多在1pbs左右。當我在設備上運行速度測試apk時,我測試的速度大約爲3.5Mbps。該功能起作用,似乎只有它的一半速度。下面的代碼應該產生準確的結果嗎?如何使用Java/Android正確測量下載速度

try { 
      String DownloadUrl = "http://ipv4.download.thinkbroadband.com:8080/5MB.zip"; 
      String fileName = "testfile.bin"; 


      File dir = new File (context.getFilesDir() + "/temp/"); 
      if(dir.exists()==false) { 
       dir.mkdirs(); 
      } 

      URL url = new URL(DownloadUrl); //you can write here any link 
      File file = new File(context.getFilesDir() + "/temp/" + fileName); 


      long startTime = System.currentTimeMillis(); 
      Log.d("DownloadManager", "download begining: " + startTime); 
      Log.d("DownloadManager", "download url:" + url); 
      Log.d("DownloadManager", "downloaded file name:" + fileName); 

      /* Open a connection to that URL. */ 
      URLConnection ucon = url.openConnection(); 

      //Define InputStreams to read from the URLConnection. 
      InputStream is = ucon.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 

      //Read bytes to the Buffer until there is nothing more to read(-1). 
      ByteArrayBuffer baf = new ByteArrayBuffer(1024); 
      int current = 0; 
      while ((current = bis.read()) != -1) { 
       baf.append((byte) current); 
      } 
      long endTime = System.currentTimeMillis(); //maybe 

      /* Convert the Bytes read to a String. */ 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(baf.toByteArray()); 
      fos.flush(); 
      fos.close(); 

      File done = new File(context.getFilesDir() + "/temp/" + fileName); 
      Log.d("DownloadManager", "Location being searched: "+ context.getFilesDir() + "/temp/" + fileName); 
      double size = done.length(); 
      if(done.exists()) { 
       done.delete(); 
      } 

      Log.d("DownloadManager", "download ended: " + ((endTime - startTime)/1000) + " secs"); 
      double rate = (((size/1024)/((endTime - startTime)/1000)) * 8); 
      rate = Math.round(rate * 100.0)/100.0; 
      String ratevalue; 
      if(rate > 1000) 
      ratevalue = String.valueOf(rate/1024).concat(" Mbps"); 
      else 
      ratevalue = String.valueOf(rate).concat(" Kbps"); 
      Log.d("DownloadManager", "download speed: "+ratevalue);  
    } catch (IOException e) { 
     Log.d("DownloadManager", "Error: " + e); 
    } 

實施例輸出

10-08 15:09:52.658: D/DownloadManager(13714): download ended: 70 secs 
10-08 15:09:52.662: D/DownloadManager(13714): download speed: 585.14 Kbps 

預先感謝您的幫助。如果有更好的方法,請告訴我。

+0

你一次讀取一個字節,這可能會很長。爲什麼不使用一個字節[]並一次讀取幾個Kb? – njzk2

+0

通常情況下,你有〜600K每秒調用'read'和'append'。 – njzk2

+0

此外,您的費率值可能不是非常精確,因爲'size','endTime'和'startTime'是整數,1024,1000和8也是如此,因此您的計算在被轉換爲long應該給出不精確的測量結果,但不對您觀察到的巨大差異負責) – njzk2

回答

2

繼我的意見,在這裏是如何從流

//Define InputStreams to read from the URLConnection. 
InputStream is = ucon.getInputStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 

//I usually use a ByteArrayOutputStream, as it is more common. 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
int red = 0; 
// This size can be changed 
byte[] buf = new byte[1024]; 
while ((red = bis.read(buf)) != -1) { 
    baos.write(buf, 0, red); 
} 

這樣做是它讀成一個byte []緩衝區中讀取幾個字節,並返回讀取的字節量的例子。這又被寫入到OutputStream中,指定要寫入的字節數量。

ByteArrayOutputStream也有一個toByteArray行爲相似。

另外,您也可以直接寫入文件,如果你考慮到文件操作寫比讀功能顯著快:

// Simply start by defining the fileoutputstream 
FileOutputStream fos = new FileOutputStream(file); 
int red = 0; 
// This size can be changed 
byte[] buf = new byte[1024]; 
while ((red = bis.read(buf)) != -1) { 
    // And directly write to it. 
    fos.write(buf, 0, red); 
} 
long endTime = System.currentTimeMillis(); //maybe 
// Flush after, as this may trigger a commit to disk. 
fos.flush(); 
fos.close(); 

此外,如果你真的只關心下載速度,它不是強制性的寫入文件或任何地方,這將是足夠的:

long size = 0; 
byte[] buf = new byte[1024]; 
while ((red = bis.read(buf)) != -1) { 
    size += red; 
} 
+0

對不起,沒有早點回來。我真的很感謝細節的迴應。我嘗試了兩種方法,並最終提出了甚至沒有寫出我真正想專注於下載速度的數據大小的建議。由於更改爲多個字節,我的結果必須接近該測試或其他速度測試。再次,再次感謝這個例子,他們完美地工作。 – Joffroi