I am making a rest api call from Android device and was really surprised looking at the difference of speeds when compared to PC. Below is the image from a rest tool on PC.
I tried few libraries like Retrofit, Volley and also regular Async task to make the same rest call from android device and noticed following response times.
(Response times with Retrofit library (Time includes converting json data to java objects)).
Test 1: 8372 Ms
Test 2: 7715 Ms
Test 3: 7686 Ms
Test 4: 10128 Ms
Test 5: 7876 Ms
(Response times with Volley. No conversion to Java Objects from Json Data )
Test 1: 6721 MS
Test 2: 6610 MS
Test 3: 6287 MS
Test 4: 6118 MS
Test 5: 6118 MS
I took System.currentTimeMillis()
before making a call and after getting the response and subtracted those values to get above response time in Android program.
It would be really helpful if some one can show me how to reduce the response time in android.
FYI: I am on Wifi and using same network both for my PC and Android device.
Here is the Retrofit Android code
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ENDPOINT)
.build();
WeatherApi weatherApi = adapter.create(WeatherApi.class);
startTime = System.currentTimeMillis();
weatherApi.getWeather(location.getLatitude(),location.getLongitude(),new Callback<WeatherInfo>() {
@Override
public void success(WeatherInfo arg0, Response arg1) {
endTime = System.currentTimeMillis();
Log.d("TAG",endTime-startTime + ":Millisecs");
setWeatherInfo(arg0);
if(weatherDialog != null && weatherDialog.isShowing())
weatherDialog.dismiss();
}
Generally, APIs that are considered high-performing have an average response time between 0.1 and one second. At this speed, end users will likely not experience any interruption. At around one to two seconds, users begin to notice some delay.
Retrofit is agnostic to the HTTP client it uses, so it tries to figure out what is the best available HTTP client it can use to perform the request.
By default Retrofit uses GSON as a converter (that too can be customized). Considering that GSON uses reflection to map JSON elements to your class attributes, the complexity of the WeatherInfo
class may very well have a performance penalty on the conversion step.
Since the differences in your case are not quite reasonable, my best guess is that GZIP is disabled. You can use RestAdapter.setLogLevel(LogLevel.FULL)
and check the Accept-Encoding
header to see if GZIP is enabled or not. If that is not the case then post more relevant code so we can give more insights.
A quick way which I'm sure you'll notice a huge difference is if you include okhttp and okhttp-urlconnection - when those are available in the classpath Retrofit will use okhttp as the client by default, which supports GZIP without any further configuration.
You can do so by adding the following dependencies to your build.gradle file:
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
Note that compression also has to be supported on the server-side, and I'm not sure if you've posted the complete output of the response headers, but it seems to be missing something like Content-Encoding: gzip
, if that is the case you should look into enabling this feature on your web server as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With