首页 > 解决方案 > 我正在开发一个应用程序来测试下载/上传速度。在速度比较方面面临一个主要问题

问题描述

所以为了下载速度,我基本上使用了广泛存在的保持计时器并下载文件的方法,如下所示。但是与 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。但这并没有给出当前的速度,对吧?提前致谢。

标签: androidperformancemobilewifi

解决方案


推荐阅读