9

I am making a restful API call from Android device to populate a list view .

I have used swagger codegen to generate my retrofit client .

Dagger 2 for dependency injection

Device : Asus Zenfone 5

I have tried to make call from postman, web and also in emulator (genymotion) it is much faster compared to a real device.

And interesting thing is that every time when I restart my phone for two to three request the response time is normal and after that getting slower.

I read some blog regarding gzip compression in okHTTP and try to implement that but no effect.

It would be really helpful if someone can help me to figure out the issue.

chubao
  • 5,871
  • 6
  • 39
  • 64
Rahul
  • 367
  • 2
  • 6
  • 27
  • please provide your web request and response code – Karan Maru Feb 18 '16 at 10:18
  • I think it is natural that your phone connection is slower. I would not bother with this too much and I will make UI code robust against this problem – Eugen Martynov Feb 18 '16 at 18:16
  • My network speed is 8Mbps so I think it is not the problem with network – Rahul Feb 19 '16 at 04:29
  • We recently added Android Volley library support to the `android` template. Please pull the latest master of [swagger-codegen](https://github.com/swagger-api/swagger-codegen) to generate Android API client with the [volley](https://github.com/mcxiaoke/android-volley) to see if other HTTP libraries will perform better in your scenario. – William Cheng Feb 19 '16 at 16:02
  • Ok I will try it and let you know – Rahul Feb 22 '16 at 04:50
  • @wing328 facing same issue with Volley too – Rahul Feb 22 '16 at 07:01
  • Maybe [cURL on android](https://play.google.com/store/apps/details?id=io.github.faywong.curl&hl=en) can help trouble shoot the issue to see if the issue is device-specified. – William Cheng Feb 22 '16 at 07:42
  • @wing328 when I clean ram and junk files the response time is getting faster . I have also noticed that if I connect my phone for USB debugging it is getting more and more slower and if I remove my data cable . Response time is normal . Is that any problem with cache – Rahul Feb 25 '16 at 07:49
  • @Rahul I'm not surprised if the response is slow given that USB debugging is on. – William Cheng Feb 25 '16 at 09:38
  • @wing328 Is this expected ? – Rahul Feb 25 '16 at 11:02

3 Answers3

4

In retrofit, it does following work:

  1. Build up the Retrofit class.
  2. Impl the Interface with DynamicProxy
  3. Parse and create the Http request accourding to the annotations.
  4. Send and receive HTTP(socket) IOs with OkHttp in ThreadPool(In Android, Network can't be done on main thread).
  5. Deserialize your Http body with some lib (eg. gson).
  6. update UI in callback.

In your phone, I think 1,2,3 won't be necessary, they can even be done on main thread or cached. In my device (Qcom615, 2Gram), it will take less than one ms.

So you need to debug with your network.

fix problem one by one:

  1. Is your server use HTTPS or no-cacahe or no-gzip? Logging your data and tell your server's partner, they can give you some advice.
  2. try faster lib for converters.
  3. improve your view's code(eg. void redraw/relayout, void a long-time job in main thread).
Miao1007
  • 944
  • 6
  • 22
  • Thanks for your time and effort . Please see my answer Issue was not with the tools and libraries used . – Rahul Mar 01 '16 at 03:36
3

Slow response has nothing to do you with your phone it may be due to several reasons. Some noticeable reason i know are mentioned below.

1) Internet connection on your device may be slower comparatively to your emulator which uses network connection of your machine (wifi or lan connected).

2) There are multiple applications using internet on your device in background. Check application running background process and stop them.

To ensure test your internet speed on your device using speedtest.net. Hope it helps you .

CodingRat
  • 1,934
  • 3
  • 23
  • 43
  • 3
    as mentioned in the above comment My network speed is 8Mbps so I think it is not the problem with network . In my condition for a response of 50kb it is taking more than a minute . So i don't this is due to network . – Rahul Feb 23 '16 at 07:18
  • @Rahul if it's taking more than a min for 50kb and your network speed is 8Mbps, then i guess there is something wrong with the implementation you have done. Also there must be a timeout implementation, if request is taking more than 25 sec say. – UMESH0492 Feb 27 '16 at 16:50
  • @UMESH0492 Please see my answer . Thanks a lot for your help and support – Rahul Feb 29 '16 at 07:42
3

When I run my app in the debugger, it was unusable slow. When I run it outside the debugger, it performs better.

So conclusion is that Mentioned problem was not related with tool and libraries that I was using . Issue was with USB debugging .

Since I was new To android development I was not aware of the performance of APP in USB debugging mode .

Rahul
  • 367
  • 2
  • 6
  • 27
  • App working slower when it's optimized for debug and especially when you debugging it in IDE. That's not an issue, overhead of debugging - that's why there is release/debug builds. – dasar Mar 01 '16 at 06:07