Android TrafficStats delivering wrong numbers
<p>I want to measure the data transferred and received for my HTTP request over the mobile network. I found the TrafficStats API in Android 2.2, but the numbers I get from this seem to be too low. I am measuring on a real device (HTC Desire), over 3G network.
Immediately before the request a compute the tx/rx data since boot time, then do a GET request, then calculate the rx/tx numbers again:</p>

<pre><code>long bytesStart = TrafficStats.getTotalTxBytes() + TrafficStats.getTotalRxBytes();
// execute request (Apache HTTP client)
client.execute(get);
long totalBytesNow = TrafficStats.getTotalTxBytes() + TrafficStats.getTotalRxBytes();
long bytesDiff = totalBytesNow - bytesStart;
</code></pre>

<p>For bytesDiff I get numbers around 1.5KB, where it should have been 3.5KB. The totalBytes number is around 5MB, which seems to be a reasonable amount of data for 10 hours that the device is running.</p>

<p>I read <a href="http://stackoverflow.com/questions/4029186/can-someone-explain-how-trafficstats-works-its-magic-in-the-android-os">here on stackoverflow</a> that the numbers come from a file, can it be that the data is not flushed to the file immediately? Or why are my numbers always too low (I checked the data size on the server and it is really larger)?
Is it a problem of my HTC Desire device (Android 2.2 by HTC), does the TrafficStats API work for anyone?</p>

<p>Thanks,
Martin</p>
