2013-10-08 41 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