【问题标题】:practice in using retrofit 2.0使用 Retrofit 2.0 的练习
【发布时间】:2016-05-01 05:41:43
【问题描述】:

我是使用改造的新手... 我可以得到idtitlethumbnail 但我无法在arraylist 上得到对象tags..?

请告诉我该怎么做...

这是我的 json:

{
  code: 200,
  result: {
  id: "7064",
  title: "my title",
  thumbnail: "http://myimage.com/image.jpg",
  tags: [
  {
    name: "php",
    slug: "php"
  },
  {
   name: "dependencies manager",
   slug: "dependencies-manager"
  },
  {
   name: "dependencies",
   slug: "dependencies"
  },
  {
   name: "packagist",
   slug: "packagist"
  }
  ]
 }
}

这是我使用 gson 序列化的模型

public class Detail extends Observable implements Serializable {
    @SerializedName("code")
    public Integer code;
    @SerializedName("result")
    public Result result;

    public class Result implements Serializable {
        @SerializedName("id")
        public String id;
        @SerializedName("title")
        public String title;
        @SerializedName("date")
        public String date;
        @SerializedName("thumbnail")
        public String thumbnail;
        @SerializedName("tags")
        public ArrayList<Tags> tags = new ArrayList<>();
    }

    public class Tags implements Serializable {
        @SerializedName("name")
        public String name;
        @SerializedName("slug")
        public String slug;
        public String getName() {
            return name;
        }
    }
}

还有我的 API

@GET("api/posts/detail/{id}")
    Call<Detail> getDetails (@Path("id") String id);

【问题讨论】:

  • 我没有发现任何错误,您遇到了什么错误?
  • 顺便说一句,请注意您的 json 中的错误 `title: "my title",`
  • 我没有收到错误,但是在 android studio arraylist 上编写代码时,“标签”在建议中不明显..
  • 我正确 -> 标题:“我的标题”...我没有收到错误,但是在 android studio arraylist 上编写代码时,“标签”在建议中不明显... Boukhrist 有你项目使用改造 2.0 处理 json 结构与我相同
  • @AhmadEfendi 为什么要为 Details 类扩展 Observable?

标签: android json gson retrofit retrofit2


【解决方案1】:

而不是 ArrayList 使用 List 并且不要初始化它。所以这将是这样的

@SerializedName("tags")
List<Tags> tags; 

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多