Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit Slower response time in real android devices when compared to emulator and web

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.

like image 592
Rahul Avatar asked Feb 18 '16 10:02

Rahul


People also ask

Should I use retrofit Android?

Retrofit is a type-safe HTTP networking library used for Android and Java. Retrofit was even better since it was super fast, offered better functionality, and even simpler syntax. Most developers since then have switched to using Retrofit to make API requests.

Why retrofit is used in Android?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services. We'll not go into the details of Retrofit 1. x versions and jump onto Retrofit 2 directly which has a lot of new features and a changed internal API compared to the previous versions.

What is retrofit in Android with example?

In Android, Retrofit is a REST Client for Java and Android by Square inc under Apache 2.0 license. Its a simple network library that used for network transactions. By using this library we can seamlessly capture JSON response from web service/web API.

Is retrofit RESTful?

Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.


1 Answers

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).
like image 88
Miao1007 Avatar answered Oct 10 '22 19:10

Miao1007