为什么与PC相比,当是响应时间(REST调用)慢于Android的? [英] Why is response time(for Rest Call) slower in Android when compared to PC?

查看:273
本文介绍了为什么与PC相比,当是响应时间(REST调用)慢于Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Android设备休息API调用和真的很惊讶与PC相比,当速度的差异看。
下面是在计算机上的其他工具的形象。

我试过像改造,凌空还要定期异步任务数库,这使得从Android设备相同的REST调用,并注意到以下的响应时间。

 (与改造图书馆响应时间(时间包括JSON数据转换为Java对象))。
测试1:8372女士
测试2:7715女士
测试3:7686女士
测试4:10128女士
测试5:7876女士(带凌空响应时间,没有转换到Java的JSON数据对象)
测试1:6721 MS
测试2:6610 MS
试验3:6287 MS
测试4:6118 MS
测试5:6118 MS

我把 System.currentTimeMillis的()拨打电话前,得到响应后减去这些值来获得高于Android程序的响应时间。

这将是非常有益的,如果有人能告诉我如何缩短响应时间的机器人。

FYI:我在WiFi和使用相同的网络都为我的PC和Android设备

下面是Android的改造code

  RestAdapter适配器=新RestAdapter.Builder()
                                .setEndpoint(端点)
                                。建立();    WeatherApi weatherApi = adapter.create(WeatherApi.class);
    STARTTIME = System.currentTimeMillis的();
    weatherApi.getWeather(location.getLatitude(),location.getLongitude(),新的回调< WeatherInfo>(){        @覆盖
        公共无效成功(WeatherInfo为arg0,ARG1响应){
            ENDTIME = System.currentTimeMillis的();
            Log.d(TAG,结束时间-的startTime +:的毫秒);
            setWeatherInfo(为arg0);
            如果(weatherDialog = NULL&放大器;!&安培; weatherDialog.isShowing())
            weatherDialog.dismiss();
        }


解决方案

改造是不可知的,它使用HTTP客户端,因此它试图弄清楚什么是它可用于执行请求的最好的HTTP客户端。

在默认情况下使用改造作为GSON转换器(太可定制)。考虑到GSON使用反射来映射JSON元素的类属性,在复杂的 WeatherInfo 类很可能对转换步骤的性能损失。

由于您的情况下,差别不是很合理,我最好的猜测是,GZIP被禁用。您可以使用 RestAdapter.setLogLevel(LogLevel.FULL),并检查的Accept-Encoding 头,看看是否启用GZIP或不。如果不是这样,那么发布更多有关code所以我们可以给更多的见解。

我敢肯定你会发现一个巨大的差别一个快速的方法是,如果你有okhttp和okhttp-URLConnection的 - 当那些在类路径中可用的改造将使用okhttp作为默认客户端,它支持GZIP没有任何进一步配置。

您可以通过添加下面的依赖关系到你的build.gradle文件,这样做的:

 编译com.squareup.okhttp:okhttp:2.4.0
编译com.squareup.okhttp:okhttp-的URLConnection:2.4.0

请注意该COM pression也对服务器端的支持,我不知道你是否已经张贴响应头的完整输出,但是似乎缺少像内容编码:gzip的,如果这是你应该考虑让你的Web服务器上这个功能,以及案件

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();
        }

解决方案

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.

这篇关于为什么与PC相比,当是响应时间(REST调用)慢于Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆