android - Retrofit 返回空对象,但 json 似乎有效

标签 android gson retrofit

这是我从端点获取的 json。我使用代理得到它,所以这正是改造得到的:

{
    "id": 1,
    "email": "jacek@gmail.com",
    "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",
}

我这样创建 RetrofitService:

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);

最后是我的模型类:

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;
}

为什么改造会返回一个空的用户对象? Json 有效,不会抛出任何错误。

最佳答案

给定返回 JSON 的模拟 API:http://www.mocky.io/v2/55867bf40258d3a80360db06

这个演示工作得很好:

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());
  }
}

编辑

从日志中我看到您的服务器响应 Content-Type: application/json。尝试让它以特定编码响应,比如:Content-Type: application/json; charset=utf-8

关于android - Retrofit 返回空对象,但 json 似乎有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962517/

相关文章:

java - 使用 Java/Jackson 解析 JSON

android - 应为 BEGIN_OBJECT 但在第 1 行第 128 列为 STRING

android - 将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

android - 关于 Retrofit 和 Gson 的 MalformedJsonException

java - 如何使用 Android 和 Retrofit 上传服务器中的图片?

java - 如何在 Android 5 上修补 services.jar?

android - 不同手机上的 OpenGL ES 纹理原点从上/左到下/左不同

Android:如何先隐藏再显示带动画效果的View?

android - 如何在两个 LinearLayout 之间动态放置 View?

Java 对象转 JSON,使用 javascript 解析