android - 我正在开发一个应用程序来测试下载/上传速度。在速度比较方面面临一个主要问题
问题描述
所以为了下载速度,我基本上使用了广泛存在的保持计时器并下载文件的方法,如下所示。但是与 Ookla 的 Speedtest 给出的速度相比,速度要低得多:
public class InternetSpeedTest extends AsyncTask<String,Void,String> {
long startTime;
long endTime;
private long takenTime;
@Override
protected String doInBackground(String... urlVar) {
startTime = System.currentTimeMillis();
Bitmap bmp = null;
try {
URL ulrn = new URL(urlVar[0]);
HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
long lengthbmp = con.getContentLength();
if (null != bmp) {
endTime = System.currentTimeMillis();
return lengthbmp + "";
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
long dataSize = Integer.parseInt(result) / 1024;
takenTime = endTime - startTime;
double s = (double) takenTime / 1000;
double totalspeed = dataSize / s;
TextView dspeed = (TextView) findViewById(R.id.filespeed_tv_ma);
fileSpeed.setText(Double.toString(totalspeed));
duration.setText(Long.toString(takenTime));
filesize.setText(Long.toString(dataSize));
}
}
}
通过 ping 主机服务器,我得到了很好的延迟。我怎样才能以任何其他方式实现或改进它?此外,我可以为上传速度做些什么。
我在某处读到使用 TrafficStats。但这并没有给出当前的速度,对吧?提前致谢。
解决方案
推荐阅读
- javascript - 如何使用 matter.js 获得类似气泡的功能?
- c# - EF core 3.1+Pomelo Scaffold-DbContext 防止视图代码生成
- azure-active-directory - 在 Azure AD 门户上,在哪里可以找到包含在 ID 令牌中以进行身份验证的用户的“子”声明值
- android - 类型 sdk.chat.core.BuildConfig 被定义多次
- python - 如何分割/裁剪重叠的字母图像python
- android - 在 Android 中使用 MutableData.value 不起作用
- sql - 使用 PatIndex 获取第二次出现的子字符串
- node.js - 如何在nodejs中将字符串转换为json?
- python - 如何使用 Python 单线从结果集中创建字典
- javascript - 通过nodemailer发送电子邮件以转发连接到gmail的电子邮件(forwardemail.net)