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.
Related
The problem is next.
In response I have JSON like
{
object: {
// a lot of different fields
}
}
I use Retrofit with gson parser. What I really need is just this object. I don't want to create class for response with the only one field. All responses server send in a such manner. As far I understand somewhere I need place simple code for fetching that one object and then use default parser for it.
Probably sorry for stupid question. I used Volley and there was quite a different approach.
Instead of creating a special class to handle this (and another special class for every other server response), just use Map<String, YourRealObjectType>. Then use this method to extract the YourRealObjectType instance for each response:
public static <T> T getFirstValue(Map<String, T> map) {
return map.values().iterator().next();
}
you can convert class into JsonObject class. then cal iterate all the elements in it one by one
#Get
ObservablegetData();
Note : use JsonObject not JSONObject
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.
How do I make Retrofit / Robospice handle my api responses in a way that I can get an empty JSON response body like: {}, but also a regular JSON response body?
Currently the empty response body initialises a new POJO, but I only want this to happen if there is actually a filled response body.
I have an object that contains three booleans, and these will always be set to false, while this shouldn't happen.
I somehow have the idea this is caused by my GSON deserializer (I use the default one), but don't know where to start for something like this.
Thanks for any help. :)
Why not using Boolean instead of boolean? That way the default value in null rather than false
Retrofit doesn't touch fields if there's no such fields in json. So init them by yourself.
public class Response {
public boolean value = true;
}
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/
I am developing a client-server Model-based application in which client is in Android and server in PHP.
I want to transfer product information for example Name,Price,Description, from client to server.
I have read that through marshaling/unmarshaling or serialization it can be achieved but all tutorials and example are in Java. But I need in Android. Please guide me to implement in Android. Or there is any other way to implement? Any example will b appreciated. Thanks.
Have a look on json. Google provides a nice Library called "Gson" for that.
To stick to your example, a json representation send from your server via http could be:
{"name":"foo", "price":"1000", "description":"this is an item description"}
In your app, you have a class MyObject.class
public class MyObject {
private String name;
private double price;
private String description;
// all your other methods
}
Then you can just do:
MyObject obj = new Gson().fromJson(jsonString, MyObject.class)
and voila, made an object out of the string in one line. Just be sure that the variables have the same name in the json representation and the class, then Gson does all the work for you. You can also make a String representation out of the object with String jsonString = new Gson().toJson(obj).
I'm using SimpleXML http://simple.sourceforge.net/
And I'm happy with it, it's a light jaxb like!