user1064249
user1064249

Reputation: 333

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..

Upvotes: 0

Views: 2353

Answers (1)

Hassan Ibraheem
Hassan Ibraheem

Reputation: 2369

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.

Upvotes: 2

Related Questions