Vladislav Aniskin Vladislav Aniskin - 4 months ago 344
Android Question

Post raw json in body retrofit2

I want post json request like that

{"jsonrpc": "2.0", "method": "testApi", "params": {
"message": "abc"
}
, "id": 1}

I read posts:

How to POST raw whole JSON in the body of a Retrofit request?

but i can'not found class TypedInput, TypedByteArray, TypedString in my retrofit2 package. Where is?

Answer

To POST a body in Retrofit, you create an object that represents this body, a class that includes String jsonrpc, String method, etc. Then, pass this object to the method that you define in your service interface and has a param with @Body annotation.

Here is an example for POST body object:

public class PostBody{

   String jsonprc;
   String method;
   Param param;

   public PostBody(...){
        //IMPLEMENT THIS
   }

   ...

   class Param{
       //IMPLEMENT THIS
   }

}