![]() ![]() Use the call instance as you would do with any other Java object. Requests with primitives are handled exactly the same as with more complex objects. String body = "plain text request body" Ĭall call = service.getStringScalar(body) Retrofit iterates through the list of available response converters and picks the first that is applicable: the scalars converter. Using the scalars converter will add the defined string value accordingly to your request body. public interface ScalarService String body) Īctually, that’s all the magic. Just using Gson for a defined String payload won’t map the data correctly and an error occurs during runtime. The service interface shown in the snippet below is just an illustration to send and receive text values. Use Primitives and Boxed Types for Requests & Response Now that you’ve the respective converter for scalars integrated, it’s time for the actual request. addConverterFactory(GsonConverterFactory.create()) addConverterFactory(ScalarsConverterFactory.create()) Please be aware that the order you’re adding response converters matters! As a rule of thumb, add Gson as the last converter to your Retrofit instance, because it says “Give it to me baby!” for every type -) Retrofit retrofit = new Retrofit.Builder() A second step is required: add the scalars converter to your Retrofit instance. Just the import of the new dependency isn’t sufficient to parse Java primitives appropriately. ![]() Synchronize your project after adding the new converter-scalars dependency: compile '2:converter-scalars:2.1.0' The scalars converter isn’t integrated by default, you need to add the dependency in your adle file. The scalars converter can be used to serialize your value for the text/plain request payload. ![]() Conversion applies to both directions: requests and responses. Within the available converters, you’ll also find a Retrofit Scalars Converter that does the job of parsing any Java primitive to be put within the request body. You can serialize and deserialize Java objects to JSON or XML or any other data format and vice versa. There are multiple existing Retrofit converters for various data formats. Retrofit and its converters got your back in both situations. Related to the actual question of how to send plain text within the request body, we’ll await a plain text in response. The described solutions apply for any Java primitive, not just string values. Actually, there are multiple solutions to achieve the desired result and we’ll show you two of them. There are situations where you just need to send plain text as the request payload. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |