如何使用JavaAndroid正确衡量下载速度

如何使用JavaAndroid正确衡量下载速度,第1张

概述我正在尝试在我的Android应用程序中执行AsyncTask类,该类分析下载和上传的网络连接速度.我现在正在下载部分,但没有得到预期的结果.我正在一个Wifi网络上进行测试,该网络始终保持15Mbps的降速/升速,但是,从我的应用程序获得的结果几乎只有1Mpbs.当我在要测试的设备上运行速度测试ap

我正在尝试在我的Android应用程序中执行AsyncTask类,该类分析下载和上传的网络连接速度.我现在正在下载部分,但没有得到预期的结果.我正在一个Wifi网络上进行测试,该网络始终保持15Mbps的降速/升速,但是,从我的应用程序获得的结果几乎只有1Mpbs.当我在要测试的设备上运行速度测试apk时,速度约为3.5Mbps.该功能起作用,似乎只是速度的一半.以下代码应该产生准确的结果吗?

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 secs10-08 15:09:52.662: D/DownloadManager(13714): download speed: 585.14 Kbps

先谢谢您的帮助.如果有更好的方法,请告诉我.

解决方法:

在我的评论之后,这是一个如何从流中读取几个字节的示例

//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 changedbyte[] buf = new byte[1024];while ((red = bis.read(buf)) != -1) {    baos.write(buf, 0, red);}

它的作用是将其读入byte []缓冲区,并返回读取的字节数.依次将其写入到OutputStream,指定要写入的字节数.

ByteArrayOutputStream还具有行为类似的toByteArray.

另外,如果您认为写入文件的 *** 作比读取功能要快得多,那么也可以直接写入文件:

// Simply start by defining the fileoutputstreamfileOutputStream fos = new fileOutputStream(file);int red = 0;// This size can be changedbyte[] 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;}
总结

以上是内存溢出为你收集整理的如何使用Java / Android正确衡量下载速度全部内容,希望文章能够帮你解决如何使用Java / Android正确衡量下载速度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1087449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存