Android 앱에서 Retrofit 2를 사용하여 WordPress API 와 통신하고 있지만 게시물에서 첨부 파일과 태그를 가져 오는 데 문제가 있습니다.
해당 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 :
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
public class Attachment {
@SerializedName("URL")
private String URL;
@SerializedName("width")
private int width;
@SerializedName("height")
private int height;
}
첨부 파일이나 태그에 중첩 된 JSON 개체를 제외하고는 모든 것이 잘 작동합니다. 첨부 파일 개체는 기본값 만 포함하고 JSON 응답의 올바른 값으로 채워지지 않습니다.
내 개조 빌더 :
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WordPressService.ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(WordPressService.class);
첨부 파일 목록을 갖고 싶지만이 문제를 어떻게 해결할 수 있는지 모르겠습니다.
attachments
및 tags
JSON
요소 를 구문 분석하려면 LinkedTreeMap
.
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;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다