Retrofit 2 — Wrapping Requests in Envelope with Custom Converter

In a previous tutorial, we've explored unwrapping response envelopes. Of course, there also are APIs where you wrap the requests into envelopes. If every request is wrapped in an envelope, this will make your app's code unnecessary complex, because you've to create the envelope for every request manually.

In this tutorial, you'll learn how to wrap every request automatically into an envelope. The resulting JSON could look like this:

{
  clientInfo: {
    androidVersion: 25,
    appVersion: 1,
    requestTime: 1493823786250
  },
  payload: {
    email: "norman@futurestud.io",
    id: 42
  }
}

However, the app code only supplies the payload, and can work directly with the Java object, and not with the awkward Envelope<T>.

Retrofit Series Overview

  1. Callbacks (Coming soon)
  2. Annotations (Coming soon)
  3. Fluent Interface with Builders (Coming soon)


Continue reading

University Enrollment Required

Future Students benefit from value packed videos and tutorials.

Enroll to receive exclusive content or sign in if you’re already a Future Student.

Enroll me for $12/mo

Explore the Library

Find interesting tutorials and solutions for your problems.

Miscellaneous