Jacek Kwiecień
Jacek Kwiecień

Reputation: 12637

Retrofit returns empty object, but json seems valid

This is the json I get from my endpoint. I got it using proxy, so this is exactly what retrofit gets:

{
    "id": 1,
    "email": "[email protected]",
    "name": "Jacek KwiecieŇĄ",
    "google_plus_id": "117434793312604634191",
    "facebook_id": null,
    "avatar_url": "https://some_url",
    "last_login_at": "2015-06-14T12:36:58.831Z",
    "created_at": "2015-06-14T12:36:58.829Z",
}

I create RetrofitService like this:

private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
        .setEndpoint(BuildConfig.SERVER_URL)
        .setConverter(new GsonConverter(new GsonBuilder().create()))
        .build();

public static final Api API = REST_ADAPTER.create(Api.class);

Finally here is my model class:

public class User {
    public long id;
    public boolean active;
    public String email;
    public String googlePlusId;
    public String facebookId;
    public String name;
    public String avatarUrl;
    public String coverPhotoUrl;
}

Why would retrofit return me an empty User object? Json is valid and no errors are thrown.

Upvotes: 2

Views: 1665

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

Given the mock API which returns your JSON: http://www.mocky.io/v2/55867bf40258d3a80360db06

This demo works just fine:

import com.google.gson.annotations.SerializedName;
import retrofit.RestAdapter;
import retrofit.http.GET;

public class RetrofitDemo {

  static class User {

    public long id;
    public boolean active;
    public String email;
    public String name;
    @SerializedName("google_plus_id") public String googlePlusId;
    @SerializedName("facebook_id") public String facebookId;
    @SerializedName("avatar_url") public String avatarUrl;
    @SerializedName("cover_photo_url") public String coverPhotoUrl;
    @SerializedName("last_login_at") public String lastLoginAt;
    @SerializedName("created_at") public String createdAt;

    @Override
    public String toString() {
      return "User{" +
          "id=" + id +
          ", active=" + active +
          ", email='" + email + '\'' +
          ", name='" + name + '\'' +
          ", googlePlusId='" + googlePlusId + '\'' +
          ", facebookId='" + facebookId + '\'' +
          ", avatarUrl='" + avatarUrl + '\'' +
          ", coverPhotoUrl='" + coverPhotoUrl + '\'' +
          ", lastLoginAt='" + lastLoginAt + '\'' +
          ", createdAt='" + createdAt + '\'' +
          '}';
    }
  }

  interface API {

    @GET("/55867bf40258d3a80360db06")
    User getUser();
  }

  public static void main(String[] args) {

    API api = new RestAdapter.Builder()
        .setEndpoint("http://www.mocky.io/v2")
        .build()
        .create(API.class);

    System.out.println(api.getUser());
  }
}

EDIT

From the logs I see your server responds with Content-Type: application/json. Try letting it respond with a specific encoding, say: Content-Type: application/json; charset=utf-8

Upvotes: 1

Related Questions