测量网络吞吐量Java/Android
我正在为Android开发一个应用程序,希望使用HTTP测量网络吞吐量。 到目前为止,我们已经完成了让HTTP访问www.google.pt页面的代码。 我测量请求前后的时间 问题是:我正在测量服务器和我的应用程序之间整个数据交换的时间,以及TCP协议的所有开销。我只需要获得我的应用程序只接收网页(有用数据)所需的时间 我使用wireshark查看请求过程中发生了什么。 有用的数据是您看到的PDU。这就是我想要的测量网络吞吐量Java/Android,java,android,http,networking,Java,Android,Http,Networking,我正在为Android开发一个应用程序,希望使用HTTP测量网络吞吐量。 到目前为止,我们已经完成了让HTTP访问www.google.pt页面的代码。 我测量请求前后的时间 问题是:我正在测量服务器和我的应用程序之间整个数据交换的时间,以及TCP协议的所有开销。我只需要获得我的应用程序只接收网页(有用数据)所需的时间 我使用wireshark查看请求过程中发生了什么。 有用的数据是您看到的PDU。这就是我想要的 公共静态字符串获取(字符串url){ InputStream InputStr
公共静态字符串获取(字符串url){
InputStream InputStream=null;
字符串结果=”;
试一试{
//创建HttpClient
HttpClient HttpClient=新的DefaultHttpClient();
//时间函数
Calendar calendarBefore=Calendar.getInstance();//实例类Calendar
int millissecondsBefore=calendarBefore.get(Calendar.毫秒);
//对给定URL发出GET请求
HttpResponse HttpResponse=httpclient.execute(新的HttpGet(url));
//时间函数
Calendar calendarAfter=Calendar.getInstance();//实例类Calendar
int millissecondsAfter=calendarAfter.get(Calendar.毫秒);
//时差计算
int-timeDiff=毫秒后-毫秒前;
字符串时差=新字符串();//时差(在之前之后)
//验证时差值的信号
if(timeDiff<0){
timeDiff=timeDiff*(-1);//在操作中反转信号
时差=整数.toString(timeDiff);
}
否则{
时差=整数.toString(timeDiff);
}
//作为inputStream接收响应
inputStream=httpResponse.getEntity().getContent();
//将输入流转换为字符串
如果(inputStream!=null)
结果=convertInputStreamToString(inputStream,timeDiff);
其他的
结果=“不起作用!”;
}捕获(例外e){
d(“InputStream”,例如getLocalizedMessage());
}
返回结果;
}
所以,我测量了很多我不需要的东西。我需要测量接收这些PDU所需的时间
如果你们知道什么可以帮我的话,我会帮上大忙的。如果你还有其他建议,我很乐意听听
谢谢大家! 这可能有助于:
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
//Get Bytes of data Consumed
long oldCount = android.net.TrafficStats.getMobileRxBytes();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
//Get Bytes of data Consumed
long newCount = android.net.TrafficStats.getMobileRxBytes();
//GetTotalData Consumed
Log.i("totalDataConsumption",(newCount-oldCount)+" Bytes");
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert input stream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream, timeDiff);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
嘿,我试过了,newCount和oldCount的值是一样的。totalDataConsumption有0个字节。即使这样,我也无法测量接收数据所需的时间。还有其他建议吗?嘿,这只适用于移动数据。我有一个价值观,现在我要看看我是否能做到这一点。那我会让你知道的。谢谢还可以试试这个android.net.TrafficStats.getTotalRxBytes(),我仍在努力获得一个合适的结果。我认为它可以工作。我试图使用AsyncTask在循环中测试接收到的数据量,并测量执行HttpRequest时的时间间隔。执行HttpRequest后,AsyncTask停止。我有点小问题,但我会解决的。但我用的是你给我的密码。如果这不能达到我想要的,我想我必须尝试FTP来测量吞吐量。谢谢@约翰穆希,请随时通知我。
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
//Get Bytes of data Consumed
long oldCount = android.net.TrafficStats.getMobileRxBytes();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
//Get Bytes of data Consumed
long newCount = android.net.TrafficStats.getMobileRxBytes();
//GetTotalData Consumed
Log.i("totalDataConsumption",(newCount-oldCount)+" Bytes");
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert input stream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream, timeDiff);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}