I am using Retrofit 2 in an Android app to communicate with WordPress API but I have problems to get the attachments and tags from posts.

我在Android应用程序中使用Retrofit 2与WordPress API进行通信,但是从帖子中获取附件和标签时遇到问题。

The corresponding JSON response looks like this:

相应的JSON响应如下所示:

{

"ID": 1,
"site_ID": 1,
"author": {
},
"tags": {
    "Doom": {
        "ID": 654,
        "name": "Doom",
        "slug": "doom",
        "description": "",
        "post_count": 53
    },
    "Ego-Shooter": {
        "ID": 73,
        "name": "Ego-Shooter",
        "slug": "ego-shooter",
        "description": "",
        "post_count": 724
    },
    "id Software": {
        "ID": 127,
        "name": "id Software",
        "slug": "id-software",
        "description": "",
        "post_count": 41
    }
}
"attachments": {
    "54344": {
        "ID": 54344,
        "URL": "",
        "guid": "",
        "mime_type": "image/jpeg",
        "width": 843,
        "height": 499
    },
    "54345": {
        "ID": 54345,
        "URL": "",
        "guid": "",
        "mime_type": "image/jpeg",
        "width": 800,
        "height": 1600
    }
}

}

}

Post.class:

Post.class:

public class Post {

    @SerializedName("ID")
    private int ID;

    @SerializedName("title")
    private String title;

    @SerializedName("content")
    private String content;

    @SerializedName("featured_image")
    private String featuredImage;

    @SerializedName("date")
    private String date;

    @SerializedName("URL")
    private String URL;

    @SerializedName("author")
    private Author author;

    @SerializedName("discussion")
    private Discussion discussion;

    @SerializedName("attachments")
    private Attachment attachments; // At this point I have problems
}

Attachment.class

Attachment.class

public class Attachment {

    @SerializedName("URL")
    private String URL;

    @SerializedName("width")
    private int width;

    @SerializedName("height")
    private int height;

}

}

Everything works fine except the nested JSON objects in attachments or tags. The Attachment object does only contain default values and is not filled with the correct values from the JSON response.

除了附件或标签中的嵌套JSON对象外,一切正常。 Attachment对象仅包含默认值,并且未使用JSON响应中的正确值填充。

My Retrofit builder:

我的改造生成器:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(WordPressService.ENDPOINT)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    return retrofit.create(WordPressService.class);

I would like to have a list of attachment objects but I do not know how I can solve this problem.

我想有一个附件对象列表,但我不知道如何解决这个问题。

1 个解决方案

#1


2

To parse the attachments and tags JSON elements use a LinkedTreeMap.

要解析附件和标记,JSON元素使用LinkedTreeMap。

Update the Post class as follows:

更新Post类如下:

public class Post {

  @SerializedName("ID")
  private int ID;

  @SerializedName("title")
  private String title;

  @SerializedName("content")
  private String content;

  @SerializedName("featured_image")
  private String featuredImage;

  @SerializedName("date")
  private String date;

  @SerializedName("URL")
  private String URL;

  @SerializedName("tags")
  LinkedTreeMap<String, Tag> tags;

  @SerializedName("attachments")
  LinkedTreeMap<String, Attachment> attachments;
}      

更多相关文章

  1. Java对象引用处理机制
  2. 如何从Java中的类名获取类对象
  3. 域对象/服务和业务逻辑层
  4. JavaScript笔记:混合对象“类”
  5. [零基础学JAVA]Java SE面向对象部分.面向对象基础(04)
  6. Java面向对象三大特性
  7. 请问java中调用一个静态方法()内传入一个对象是表示什么意思?
  8. Java记录 -88- 利用反射机制调用对象的私有方法和属性
  9. Java开发水吧点饮品系统(顶层父类对象做接口参数)

随机推荐

  1. ANDROID EMULATOR 4 启动报错
  2. Android SDK API 15 各安装包下载地址
  3. Android TextView使用权重时文字无法居中
  4. 2.4.12 画廊视图
  5. Android之Animation属性配置文件
  6. ch023 Android ContentProvider(第一部分
  7. Android配置文件中标签
  8. Android单元测试
  9. android全屏显示
  10. Android多点触控