Retrofit/Volley

Retrofit/Volley

Web service calling the client for?Android apps

Moving forward in the world of digitalization every app needs to communicate with the server, It's you who choose the way to communicate to the server from your apps, It makes a lot of sense to use the proper gateway to communicate from the server. The user does not want to delay in response from the server or low performance of the app due to API calls.

Let's discuss the 2 most useful clients for API calls.

Retrofit

Retrofit?can be fit in any situation of API calls whether it is rest or soap it is an open-source lib from the square.

Retrofit?is a type-safe HTTP client that enables the communication between your app and API server, Retrofit turns your HTTP request into interfaces, It can have both synchronous and asynchronous calls of API.

Retrofit?is the class through which your API interfaces are turned into callable objects. By default, Retrofit will give you sane defaults for your platform but it allows for customization.

By default, Retrofit can only deserialize HTTP bodies into OkHttp’s?ResponseBodytype and it can only accept its?RequestBody?type for?@Body.

No alt text provided for this image

Some Advantages of Retrofit:

  1. Get rid of managing requests and responses manually,
  2. Get rid of managing thread and background processes by yourself.
  3. Get rid of the boilerplate and unreliable async task code, Overall maintains clean code, and Each part of the code will have a single responsibility.
  4. Get the parsing of your response done by lib itself, Retrofit will save your development time.
  5. Most of the things can be customized according to our needs.
  6. Better support for Rx android and kotlin coroutines libs.
  7. By default follows the MVP pattern so simple and easy to understand.
  8. The response we get is very straight forward error codes and success codes are pretty well handled.
  9. Retrofit makes it much easier to configure HTTP intercepts (if you want to do something before or after an HTTP call).
  10. By default uses disk cache(If we enable caching).

Some Limitations of Retrofit:

  1. Tightly coupled with the response type.
  2. Internally uses OKHTTP for HTTP calls.
  3. It does get a successful response if we touch the server so if any error is there so we have to get from the response code and behave accordingly.
  4. In other words, only get a failure response if the server is not reachable or some problem is there in response parsing.
  5. For image, loading retrofit is not having tags as we have in the volley.
  6. Retrofit:?caching should “just work”?if your server sets the correct cache-control headers. If not, or if you’re trying to do something unusual, you may be out of luck.
  7. Retrofit: does not support setting priority, although?there are hacks available;?supports cancellation since v2?(which is in late beta now);?supports manual request retries.
  8. Internal thread pool logic is not exposed to clients.

Volley

Volley?is an?HTTP library?that makes networking very easy and fast, for Android apps. It was developed by Google and introduced during Google I/O 2013. It was developed because there is an absence in Android SDK, of a networking class capable of working without interfering with the user experience. Although Volley is a part of the Android Open Source Project(AOSP), Google announced in January 2017 that Volley will move to a standalone library. It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again.

No alt text provided for this image

Features

  1. Request queuing and prioritization.
  2. Effective request cache and memory management.
  3. Extensibility and customization of the library to our needs.
  4. Cancelling the requests.
  5. Automatic scheduling of network requests.
  6. Multiple concurrent network connections.
  7. Transparent disk and memory response caching with standard HTTP?cache coherence.
  8. Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
  9. Volley provides debugging and tracing tools.

10. Provides image and JSON object, JSON array, and String request types.

Limitations of Volley

  1. Complex to implement for beginners.
  2. Have default caching which we might not need in our projects.
  3. It does not give the response code we need to extract.
  4. I need to parse the response manually by writing the code.
  5. Need to convert JSON object JSON array for request.

Conclusion

If we compare both so both are having advantages and disadvantages. If performance-wise both are almost the same so we can choose one of them, but in my perspective, I would go for Retrofit because it is easy to use and separation of code is easy in retrofit, We need to write less code in the retrofit.

Ali Moradi

Senior Android Developer

2 年

Android fast networking

回复
Ali Moradi

Senior Android Developer

2 年

Retrofit

回复
Amer Da'ajeh

Modern Android Developer, Kotlin Developer, KMP Developer, Software Engineer, Compose <3

2 年

None of the above, i use ktor

回复

要查看或添加评论,请登录

Aalishan Ansari的更多文章

  • Android Activity Frequently Asked Interview Questions Part-1

    Android Activity Frequently Asked Interview Questions Part-1

    1. What is the activity?/What do you know about the activity? Ans: Activity is one of the building blocks of Android…

    6 条评论
  • MVP Architecture with Example in Android

    MVP Architecture with Example in Android

    In this blog, we will take an example of a login screen in Android and cover up MVP very simply. As shown in the above…

    2 条评论
  • Activity lifecycle callbacks A →B and B →A

    Activity lifecycle callbacks A →B and B →A

    Suppose you are having one more activity called C If you kill Activity C from the task manager so onDestroy(C)…

    1 条评论
  • Fragment to Fragment Communication Using ViewModel

    Fragment to Fragment Communication Using ViewModel

    In MainActivity class MainActivity : AppCompatActivity() { private var _activityMainBinding: ActivityMainBinding?…

    2 条评论
  • S.O.L.I.D Principles (Examples in Kotlin)

    S.O.L.I.D Principles (Examples in Kotlin)

    Lots of developers do the code, but A good developer is one who takes care of the code with respect to many programming…

    1 条评论
  • Design Patterns (Very simple examples)

    Design Patterns (Very simple examples)

    In the software industry, most common problems have common solutions, and that solution follows common types of…

    1 条评论
  • References Role in Garbage Collection

    References Role in Garbage Collection

    In Java, there are four types of references differentiated by the way by which they are garbage collected. Strong…

  • Activity|LaunchMode|With Lifecycle

    Activity|LaunchMode|With Lifecycle

    Before starting development in any platform first, we should understand the ecosystem of the platform. If you are…

    2 条评论
  • OOPs Concepts (Examples in Kotlin)

    OOPs Concepts (Examples in Kotlin)

    Object-Oriented Programming in detail. The Oops concepts are the foundation when you want to create a good application…

  • Rx Android

    Rx Android

    Rx Android is a concept used in android app development to give app better performance. Reactive Extension is a library…

    1 条评论

社区洞察

其他会员也浏览了