我正在使用Speed-Test-Lib进行下载速度测试。
当我尝试 DownloadFileExample.java 与host =“ download.thinkbroadband.com ”& uri =“ /20MB.zip ”,从PC上正确下载文件。
但是,当我在移动设备上使用相同的主机和uri尝试 android示例时,文件会在下载一些Mega字节后开始下载然后停止,就像主机一样正在等待的东西,然后当然超时了。
手机的下载速度约为2.4Mbps,对于PC,我将手机用作热点。
我知道这个问题与我使用的库无关,因为我也尝试过使用套接字编写一次,而另一个使用HTTPUrlConnection进行编码,但问题相同。
任何人都可以帮助我知道这个问题的原因。
DownloadFileExample.java
package fr.bmartel.speedtest.examples;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.SpeedTestSocket;
import fr.bmartel.speedtest.inter.ISpeedTestListener;
import fr.bmartel.speedtest.model.SpeedTestError;
import fr.bmartel.speedtest.model.SpeedTestMode;
/**
* Download file Speed Test example.
*
* @author Bertrand Martel
*/
public class DownloadFileExample {
/**
* socket timeout used in ms.
*/
private final static int SOCKET_TIMEOUT = 5000;
/**
* speed examples server host name.
*/
private final static String SPEED_TEST_SERVER_HOST = "download.thinkbroadband.com";
/**
* spedd examples server uri.
*/
private final static String SPEED_TEST_SERVER_URI_DL = "/20MB.zip";
/**
* speed examples server port.
*/
private final static int SPEED_TEST_SERVER_PORT = 80;
/**
* logger.
*/
private final static Logger LOGGER = LogManager.getLogger(DownloadFileExample.class.getName());
/**
* Download file example main.
*
* @param args no args required
*/
public static void main(final String[] args) {
// instantiate speed examples
final SpeedTestSocket speedTestSocket = new SpeedTestSocket();
//set timeout for download
speedTestSocket.setSocketTimeout(SOCKET_TIMEOUT);
// add a listener to wait for speed examples completion and progress
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadFinished(final SpeedTestReport report) {
LogUtils.logFinishedTask(SpeedTestMode.DOWNLOAD, report.getTotalPacketSize(),
report.getTransferRateBit(),
report.getTransferRateOctet(), LOGGER);
}
@Override
public void onDownloadError(final SpeedTestError speedTestError, final String errorMessage) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Download error " + speedTestError + " : " + errorMessage);
}
}
@Override
public void onUploadFinished(final SpeedTestReport report) {
LogUtils.logFinishedTask(SpeedTestMode.UPLOAD, report.getTotalPacketSize(),
report.getTransferRateBit(),
report.getTransferRateOctet(), LOGGER);
}
@Override
public void onUploadError(final SpeedTestError speedTestError, final String errorMessage) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Upload error " + speedTestError + " : " + errorMessage);
}
}
@Override
public void onDownloadProgress(final float percent, final SpeedTestReport downloadReport) {
LogUtils.logSpeedTestReport(downloadReport, LOGGER);
}
@Override
public void onUploadProgress(final float percent, final SpeedTestReport uploadReport) {
LogUtils.logSpeedTestReport(uploadReport, LOGGER);
}
@Override
public void onInterruption() {
//triggered when forceStopTask is called
}
});
speedTestSocket.startDownload(SPEED_TEST_SERVER_HOST, SPEED_TEST_SERVER_PORT, SPEED_TEST_SERVER_URI_DL);
}
}
使用Android(AsyncTask):
package ac.mohammad.com.tmpapp2;
import android.os.AsyncTask;
import android.util.Log;
import fr.bmartel.speedtest.SpeedTestReport;
import fr.bmartel.speedtest.SpeedTestSocket;
import fr.bmartel.speedtest.inter.ISpeedTestListener;
import fr.bmartel.speedtest.model.SpeedTestError;
/**
* Created on 014 2/14/2017.
*/
public class TestTask extends AsyncTask<Integer, Double, String> {
@Override
protected void onProgressUpdate(Double... progress) {
}
@Override
protected void onPostExecute(String file_url) {
Log.d("speedtest","Finish");
}
@Override
protected String doInBackground(Integer... params) {
SpeedTestSocket speedTestSocket = new SpeedTestSocket();
// add a listener to wait for speedtest completion and progress
speedTestSocket.addSpeedTestListener(new ISpeedTestListener() {
@Override
public void onDownloadFinished(SpeedTestReport report) {
// called when download is finished
Log.v("speedtest", "[DL FINISHED] rate in octet/s : " + report.getTransferRateOctet());
Log.v("speedtest", "[DL FINISHED] rate in bit/s : " + report.getTransferRateBit());
}
@Override
public void onDownloadError(SpeedTestError speedTestError, String errorMessage) {
// called when a download error occur
Log.v("speedtest", "[DL Error] : " + errorMessage);
}
@Override
public void onUploadFinished(SpeedTestReport report) {
// called when an upload is finished
Log.v("speedtest", "[UL FINISHED] rate in octet/s : " + report.getTransferRateOctet());
Log.v("speedtest", "[UL FINISHED] rate in bit/s : " + report.getTransferRateBit());
}
@Override
public void onUploadError(SpeedTestError speedTestError, String errorMessage) {
// called when an upload error occur
Log.v("speedtest", "[UL Error] : " + errorMessage);
}
@Override
public void onDownloadProgress(float percent, SpeedTestReport report) {
// called to notify download progress
Log.v("speedtest", "[DL PROGRESS] progress : " + percent + "%");
long curTime =System.currentTimeMillis();
}
@Override
public void onUploadProgress(float percent, SpeedTestReport report) {
// called to notify upload progress
Log.v("speedtest", "[UL PROGRESS] progress : " + percent + "%");
Log.v("speedtest", "[UL PROGRESS] rate in bit/s : " + report.getTransferRateBit());
}
@Override
public void onInterruption() {
// triggered when forceStopTask is called
Log.v("speedtest", "[Forced Stop]");
}
});
speedTestSocket.startDownload("download.thinkbroadband.com", "/20MB.zip");
return null;
}
}
android AsyncTask的调用如下:
new TestTask().execute();