如何在JAVA中获得上传速度

时间:2014-01-29 05:37:37

标签: java android tomcat servlets

我正在学习Android应用程序,我想构建示例代码来检查上传速度。

我已经建立了上传速度检查方法,但我不确定它是否正常工作。这是因为我的方法即使Android手机没有正确的互联网连接也会返回上传速度

我建造了类似的方法。

首先,读取4.6 MB文本文件,然后存储到String变量

其次,检查开始时间

第三,使用post方法和nameValuePair将4.6MB字符串变量发送到谷歌应用引擎(我的谷歌应用引擎。它捕获字符串数据。它没有做任何事情。)

第四,检查结束时间

最后,获取(文件大小)/(结束时间 - 开始时间)作为上传速度

这是我的代码......

public double Get_UPload_Speed() throws IOException{
    String file_path =("46.txt");
    File gone = new File(file_path);
    String line = "";
    double filesize = bts.Get_File_Size(file_path);
    int maxsize =  rf.Get_Total_Line(file_path);
    BufferedReader in = new BufferedReader(new FileReader(gone));

    HttpPost httpPost = new HttpPost("http://google.app.engine.address.com");
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    StringBuilder stringBuilder = new StringBuilder();


    for(int index = 0 ; index < maxsize ; index++){
        if((line = in.readLine()) != null){
            // keep appending line of text to String buffer
            stringBuilder.append(line);         
        }
        else{
            break;
        }
    }
    String UL_Packet = stringBuilder.toString();
    double startTime = System.currentTimeMillis();
    nameValuePair.add(new BasicNameValuePair("content",UL_Packet));
    nameValuePair.add(new BasicNameValuePair("guestbookName", "default"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            Log.d("UploadManager", "Upload Start: " + startTime + " secs");
        Log.d("UploadManager", "Upload End: " + endTime + " secs");
        Log.d("UploadManager", "Tranfer Time: " + tranferTime + " secs");
        Log.d("UploadManager", "Size of file " + filesize + " MB");
        double endTime = System.currentTimeMillis();
        double tranferTime = (endTime - startTime)/1000;
        double rate = filesize/ tranferTime;         
        Log.d("UploadManager", "Upload speed: "+rate +" Mbps"); 
        return rate;
    }

但是,我不确定这是否是获得上传速度的正确方法。

有没有可靠的方法来构建上传速度检查方法?

感谢

1 个答案:

答案 0 :(得分:2)

您是否尝试使用Android TrafficStats

/** Upload a random string and measure transfer speed */
public double Get_UPload_Speed()
    throws IOException
{
    int        MB         = 1;
    HttpPost   httpPost   = new HttpPost("http://google.app.engine.address.com");
    HttpClient httpClient = DefaultHttpClient();

    /** generate random string */
    char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890";
    StringBuilder sb     = new StringBuilder();
    Random        random = new Random();
    for (int i = 0; i < MB*1024*1024; i++) {
        sb.append(
                chars[random.nextInt(chars.lenght)]);
    }

    long totalTxBytes = TrafficStats.getTotalTxBytes(); 
    long totalRxBytes = TrafficStats.getTotalRxBytes();
    double startTime  = System.currentTimeMillis();

    String UL_Packet = sb.toString();
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(
            new BasicNameValuePair("content",UL_Packet));
    nameValuePair.add(
            new BasicNameValuePair("guestbookName", "default"));
    httpPost.setEntity(
            new UrlEncodedFormEntity(nameValuePair));

    httpClient.execute(httpPost);

    double endTime = System.currentTimeMillis();
    double tranferTime = (endTime - startTime)/1000;

    double rate = 
        (  TrafficStats.getTotalTxBytes() - totalTxBytes
         + TrafficStats.getTotalRxBytes() - totalRxBytes) / tranferTime;         

    /** Log */
    Log.d("UploadManager", "Upload Start: " + startTime            );
    Log.d("UploadManager", "Upload End:   " + endTime              );
    Log.d("UploadManager", "Tranfer Time: " + tranferTime + " secs");
    Log.d("UploadManager", "Data size:    " + MB          + " MB  ");
    Log.d("UploadManager", "Upload speed: " + rate        + " MB/s"); 

    return rate;
}