This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?
See similar question here. Or is this answer correct that it must be form url encoded and passed as a field? I really hope not, as the services I am connecting to are just expecting raw JSON in the body of the post. They are not set up to look for a particular field for the JSON data.
I just want to clarify this with the restperts once and for all. One person answered not to use Retrofit. The other was not certain of the syntax. Another thinks yes it can be done but only if its form url-encoded and placed in a field (that's not acceptable in my case). No, I can't re-code all the services for my Android client. And yes, it's very common in major projects to post raw JSON instead of passing over JSON content as field property values. Let's get it right and move on. Can someone point to the documentation or example that shows how this is done? Or provide a valid reason why it can/should not be done.
UPDATE: One thing I can say with 100% certainty. You CAN do this in Google's Volley. It's built right in. Can we do this in Retrofit?
Using JsonObject is the way it is:
Create your interface like this:
Make the JsonObject acording to the jsons structure.
Call the service:
};
And that its! In my personal opinion, its a lot better than making pojos and working with the class mess. This is a lot more cleaner.
I particularly like Jake's suggestion of the
TypedString
subclass above. You could indeed create a variety of subclasses based on the sorts of POST data you plan to push up, each with its own custom set of consistent tweaks.You also have the option of adding a header annotation to your JSON POST methods in your Retrofit API…
…but using a subclass is more obviously self-documenting.
use following to send json
and pass it to url
Yes I know it's late, but somebody would probably benefit from this.
Using Retrofit2:
I came across this problem last night migrating from Volley to Retrofit2 (and as OP states, this was built right into Volley with
JsonObjectRequest
), and although Jake's answer is the correct one for Retrofit1.9, Retrofit2 doesn't haveTypedString
.My case required sending a
Map<String,Object>
that could contain some null values, converted to a JSONObject (that won't fly with@FieldMap
, neither does special chars, some get converted), so following @bnorms hint, and as stated by Square:So this is an option using
RequestBody
andResponseBody
:In your interface use
@Body
withRequestBody
In your calling point create a
RequestBody
, stating it's MediaType, and using JSONObject to convert your Map to the proper format:Hope this Helps anyone!
An elegant Kotlin version of the above, to allow abstracting the parameters from the JSON convertion in the rest of your application code:
Instead of classes we can also directly use the
HashMap<String, Object>
to send body parameters for exampleAfter so much effort, found that the basic difference is you need to send the
JsonObject
instead ofJSONObject
as parameter.