【问题标题】:parsing json to POJO using retrofit使用改造将 json 解析为 POJO
【发布时间】:2016-06-20 05:56:21
【问题描述】:

我正在尝试使用改造将 json 解析为 POJO,但不断出现错误。 它似乎得到了数据,据我从 logcat 中看到的,我正确读取并解析了 json。但它返回 succes=false 并且不返回 responsedata 对象。 错误类型和错误消息为 null。 我不知道出了什么问题,如果有任何解决方案或如何定位错误的建议,我将不胜感激。

json

{
  "competition_info": {
    "headline": "competition",
    "description": "description for competition.",
    "accept_button": "OK",
    "open_terms_button": "Open info"
  },
  "terms": {
    "html": "a website in html"
  }
    }

MyPojo

public class AnnComResponses{
    @SerializedName("competition_info")
    public CompetitionInfo competitionInfo;
    @SerializedName("terms")
    public Terms terms;

    public class Terms{

        @SerializedName("html")
        public String html;

        public String getTerms() {
            return html;
        }
    }

    public class CompetitionInfo{
        @SerializedName("headline")
        public String headline;
        @SerializedName("description")
        public String description;
        @SerializedName("accept_button")
        public String acceptButton;
        @SerializedName("open_terms_button")
        public String openTermsButton;

        public String getHeadline() {
            return headline;
        }

        public String getDescription() {
            return description;
        }

        public String getAcceptButton() {
            return acceptButton;
        }

        public String getOpenTermsButton() {
            return openTermsButton;
        }

    }
}

【问题讨论】:

标签: java android json retrofit pojo


【解决方案1】:

首先,您的主类中有几个类。将它们分成单独的文件,不要将它们放在同一个类中。第二个在你的主类中,你需要为你的属性 CompetitionInfo 和 terms 添加 setter 和 getter。同样作为一种好的样式,将属性设为私有,而您的 getter 和 setter 保持公开。

【讨论】:

    【解决方案2】:

    尝试使用 GSON 转换器进行如下改造:

    RestAdapter restAdapter = new RestAdapter.Builder()
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setConverter(new GsonConverter(new GsonBuilder()).create()))
                    .setEndpoint("your api end point goes here")
                    .build(); 
    YourAPI api = restAdapter.create(YourAPI.class);
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2015-09-10
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2018-02-22
      • 2017-09-06
      • 1970-01-01
      相关资源
      最近更新 更多