我正在研究android应用,我想构建示例代码来检查上传速度。
我已经建立了上传速度检查方法,但是我不确定它是否可以正常工作。这是因为即使android手机没有正确的互联网连接,我的方法也会返回上传速度
我建立了方法..
首先,读取4.6 MB的文本文件,然后将其存储到String变量中
二,检查开始时间
第三,使用post方法和nameValuePair将4.6MB字符串变量发送到Google App Engine(我的Google App Engine。它正在捕获字符串数据。它什么都不做。)
四,检查结束时间
最后,以(文件大小)/(结束时间-开始时间)作为上传速度
这是我的代码...
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;
}
但是,我不确定这是否是提高上传速度的正确方法。
有什么可靠的方法可以建立上传速度检查方法?
谢谢
最佳答案
您是否尝试使用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;
}