How to calculate website speed in Android
How can I calculate the website speed in Android, in other words how soon it will open up in browser. I also wish to calculate all the statistics of that particular page e.g. t开发者_如何转开发otal bytes or connection time etc.
You could:
- save the current timestamp,
- make a http request,
- get the http response,
- and get the timestamp as soon as your response arrives and calculate the difference in time.
Or, you could
- Create a WebView
- Set a WebViewClient
- Use its callbacks - onPageLoaded, onPageFinished, onPageStarted, etc ...
Check here: http://developer.android.com/reference/android/webkit/WebViewClient.html
The logic may not be very accurate, but this is what I think:
- You open an
HttpConnection
to that website. - Take the current time-stamp somewhere in any variable
- From that
HttpConnection
, start reading the contents of the website, and store it all into a string variable - Again take current time-stamp somewhere
Now, Analysis:
- The difference between both the time-stamps is the time that it takes to open that website based on the current speed.
- The size of the String variable you downloaded the whole content is the size of the home-page of that website
size/time taken
is your current internet speed
This might not work accurately in some situations, like when the URLs are being redirected, i guess.
Its still what I think, maybe better options are available.
Hope it helps.
精彩评论