Pra*_*ash 17 rest android android-asynctask android-volley retrofit
我正在从Android设备进行休息api调用,并且与PC相比,看到速度的差异真的很惊讶.下面是PC上休息工具的图像.
我尝试了很少的库,比如Retrofit,Volley和常规的异步任务来从Android设备进行相同的休息调用,并注意到响应时间.
(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
Run Code Online (Sandbox Code Playgroud)
我System.currentTimeMillis()在拨打电话之前收到了响应并减去了这些值,以便在Android程序中获得更高的响应时间.
如果有人能告诉我如何减少android中的响应时间,那将非常有用.
仅供参考:我在使用Wifi并在我的PC和Android设备上使用相同的网络.
这是Retrofit Android代码
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();
}
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
请注意,服务器端也必须支持压缩,我不确定您是否已发布响应标头的完整输出,但它似乎缺少类似的内容Content-Encoding: gzip,如果是这种情况,您应该看看在您的网络服务器上启用此功能。
| 归档时间: |
|
| 查看次数: |
3335 次 |
| 最近记录: |