【问题标题】:how to get object with retrofit library?如何使用改造库获取对象?
【发布时间】:2015-08-20 11:06:56
【问题描述】:

我是第一次尝试使用改造库,但遇到了问题。

这是我的 JSON:

{  
   "id":"15",
   "email":"example@gmail.com",
   "phone":null,
   "password":"",
   "login":"",
   "userProfile":{ 
      "lan_status":"",
      "name":"example",
      "sex":"0",
      "second_name":"",
      "date_of_birth":"0000-00-00 00:00:00"
   }
}

这是我的改造代码:

String url = "https://example.com";
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(url)
                .build();

        mServerApi = restAdapter.create(ServerApi.class);
mServerApi.getUserInformation( new Callback <GetUserProfileInfo> () {
            @Override
            public void success(GetUserProfileInfo getUserProfileInfo, Response response) {
                saveUserInfromationToDb(mActivity, getUserProfileInfo);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });

public class GetUserProfileInfo {
    @SerializedName("id")
    public int mUserId;
    @SerializedName("email")
    public String mUserEmail;
    @SerializedName("phone")
    public String mUserPhone;
    @SerializedName("password")
    public String mUserPassword;
    @SerializedName("login")
    public String mUserLogin;
    @SerializedName("userProfile")
    public List<GetUserProfileInfo_2> userProfile;

}

public class GetUserProfileInfo_2 {
    @SerializedName("lanStatus")
    public int mLanStatus;
    @SerializedName("firstName")
    public String mFirstName;
    @SerializedName("lastName")
    public String mLastName;
    @SerializedName("sex")
    public int mSex;
}

但我遇到了这个问题: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_ARRAY 但在第 1 行第 533 列路径 $.userProfile 中是 BEGIN_OBJECT

很高兴得到任何帮助!

【问题讨论】:

  • 你能发布你的 API URL 吗?
  • userProfile 不是列表/数组
  • 尝试@SerializedName("userProfile") public GetUserProfileInfo_2 userProfile;而不是 @SerializedName("userProfile") public List userProfile;
  • 一个好问题,不值得 2 票反对

标签: android json serialization retrofit


【解决方案1】:
public class GetUserProfileInfo {
    @SerializedName("id")
    public int mUserId;
    @SerializedName("email")
    public String mUserEmail;
    @SerializedName("phone")
    public String mUserPhone;
    @SerializedName("password")
    public String mUserPassword;
    @SerializedName("login")
    public String mUserLogin;
    @SerializedName("userProfile")
    public GetUserProfileInfo_2 userProfile;

}

【讨论】:

    【解决方案2】:

    根据您的 JSON,userProfile 必须是对象,而不是列表。使用来自userProfile 的所有这些字段创建另一个类,并使用这个类而不是List&lt;GetUserProfileInfo_2&gt; 来代替userProfile。

    【讨论】:

      【解决方案3】:

      查看我使用 Stackoverflow REST API 制作的示例应用程序可能会让您受益。我从 Retrofit 调用中得到的对象是一个对象列表,所以它看起来很适合您的需求!

      https://github.com/BrunoCa/stackrest 这就是应用程序:https://play.google.com/store/apps/details?id=bruno.stackrest

      享受

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 2015-12-07
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-29
        • 2016-07-07
        相关资源
        最近更新 更多