Retrofit2 post JSON with field name - android

Trying to POST something like this with GSON and retrofit:
{user: {"email":"asdfghj#wedssd.jk","name":"fjnhfioewhifhjierj","password":"password""} }
instead of
{"email":"asdfghj#wedssd.jk","name":"fjnhfioewhifhjierj","password":"password"}
TypedInput and TypedByteArray removed in 2.0, how this is possible?

Create class with name User with variables email,name,password.
Create a Class with any name xyz having Object of User.
After that Parse xyz class for Gson with Retrofit.

Related

send the object containg list of same type of objects with to server using retrofit 2 POST

How do I send the following object to server using retrofit 2:
{"list":[
{
"addrress1":
{"addressLine1":"EktaColony",
"addressLine2":"Warje",
"country":"India",
"state":"Maharashtra",
"city":"Pune",
"zipcode":411058},
},
{address2:{.....,.....,...}}
]}
I am using Rxjava.
You can send the json as body of HTTP request. Retrofit provides the annotation #Body for its use
So in your interface
#POST("/yourserver/api")
Observable<ResponseType> sendReq(#Body RequestParser parser);
Your RequestParser Object is the object mapped from the json string. You can use any Json serializer libraries like gson or jackson for it.

Custom Retrofit converter to nest JSON

I have a POJO that contains a single authentication token in a string variable. I need to send this to my API using the following template:
{ “auth” : { api_key”:”XXXX” } }
I am using moshi to convert my POJOs to JSON, which returns
{"api-key":"XXXX"}
How can I add the extra brackets on the outside of my current one using a retrofit converter?
You can use Moshi-Lazy-Adapters to wrap the json. Just use the following annotation when you send the body:
#POST("your_post_url")
Call<YourResponseTypeHere> sendApiKey(#Body #Wrapped({"auth", "api_key"}) String apiKey);
The library's adapter will do the rest. And you don't need to declare an extra object.
I think you should use oAuth authentication. Then, you can use an interceptor for Retrofit and populate that token.

How to post JSON Array to server in android via retrofit2?

How do I post a json array with retrofit2?
For example,
http://www.test.com/post/listparam=[{id=1,name="A"},{id=2,name="B"}]
If you want to pass any json,pass pojo class or bean class
For Example if your json is like this
{
"main":{"id":"1","name":"A"}
}
Then paste your json string in jsonshematopojo.org,
In source type click JSON radio button,In Annotation style select Gson and also click include constructers(You can download your generated bean classes by clicking Zip button).Then instead of passing json pass like this
new Example(new Main(id,name));
Instead of declaring String parameter in interface declare Example(Your main bean class name

Convert POJO to JSON Retrofit in Android

Is there any simple way in Retrofit to convert passed object to JSON automatically?
This is my Retrofit interface :
#FormUrlEncoded
#POST("/places/name")
void getPlacesByName(#Field("name") String name,
#Field("city") String city,
#Field("tags") Tags tags,
Callback<PlaceResponse> callback);
At first I thought that if I pass Tags object it will be automatically converted to JSON , but in reality request looks like this :
name=pubs&city=London&tags=com.app.database.model.Tags%4052aa38a8
Is there any simple way to convert POJO to JSON in Retrofit?
You are creating a URL with parameters because you're using the #URLEncoded and passing the parameters as #Field.
Here is the solution:
#POST("/places/name")
void getPlacesByName(#Body RequestObject req, Callback<PlaceResponse> callback);
Additionaly, I would advise on using #GET for retrieving objects. #POST is used for creating an object and #PUT for updating. Although this isn't wrong, it as recomendation in order be RESTful compliant.
Use Jackson to convert it directly to a String:
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
Or use Gson: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Retrofit returns an empty array

I can't get a grasp on Retrofit.. I have an api that returns a result like this: {"user":[{"id":"11","username":"jason95","password":"9355a70301e214efa92b0c5a75be3d29"}]}
This is my interface: http://notepad.cc/retrointerface
This is my code for the callback: http://notepad.cc/retrocallback
If anyone can point me to the correct way to Retrofit, I would GREATLY appreciate it..
By default, Retrofit uses Gson to parse and map the response body from the server to your result object. With the response you're expecting, you need to write a class that maps the JSON object to a Java object. Something like below:
public class ResponseObject {
User[] user;
class User {
String id;
String username;
String password;
}
}
You need to read more about Gson to get that part right. Here's a nice tutorial about using Retrofit with Gson: http://engineering.meetme.com/2014/03/best-practices-for-consuming-apis-on-android/
You could use something other than Gson, but Gson works with Retrofit without any hassle, and will probably fit your needs perfectly.

Categories

Resources