gpt4 book ai didi

java - Android XML 绑定(bind) - Simpleframework

转载 作者:行者123 更新时间:2023-12-01 05:45:34 27 4
gpt4 key购买 nike

我正在尝试使用 Simpleframework 将 XML 绑定(bind)到 POJO但它无法正常工作,我无法发现错误。这个结构与 JAXB 完美配合,我只是将注释更改为 Simpleframework 的。也许错误是在List<Track>中事情,但我无法弄清楚。

Message.toString 写入此内容( header 已正确映射):

Message [body=Body [trackList=TrackList [tracks=[Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null]]]], header=Header [statusCode=200, executeTime=0.0075960159301758, available=1]]

这是传入的 XML:

<message>
<header>
<status_code>200</status_code>
<execute_time>0.0075960159301758</execute_time>
<available>1</available>
</header>
<body>
<track_list>
<track>
<track_id>4110618</track_id>
<track_mbid>888d13a8-5bbb-44d8-b3c9-5c3812020110</track_mbid>
<track_length>0</track_length>
<lyrics_id>6283932</lyrics_id>
<instrumental>0</instrumental>
<subtitle_id>0</subtitle_id>
<track_name>Stan</track_name>
<album_name>How to Be an MC, Volume 13 (Eminem Instrumentals)</album_name>
<album_id>10558107</album_id>
<artist_id>426</artist_id>
<album_coverart_100x100>http://api.musixmatch.com/albumcover/10558107.jpg</album_coverart_100x100>
<artist_mbid>b95ce3ff-3d05-4e87-9e01-c97b66af13d4</artist_mbid>
<artist_name>Eminem</artist_name>
</track>
</track_list>
</body>

这些是我正在使用的类:

package org.lyrics.xml.binding.track;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "message")
public class Message {

@Element(name = "header", required = false)
public Header header;
@Element(name = "body", required = false)
public Body body;

public Body getBody() {
return body;
}

public void setBody(Body body) {
this.body = body;
}

public Header getHeader() {
return header;
}

public void setHeader(Header header) {
this.header = header;
}

@Override
public String toString() {
return "Message [body=" + body + ", header=" + header + "]";
}

}
<小时/>
package org.lyrics.xml.binding.track;

import org.simpleframework.xml.Element;

public class Header {

@Element(name = "status_code", required = false)
public String statusCode;
@Element(name = "execute_time", required = false)
public String executeTime;
@Element(name = "available", required = false)
public String available;

public String getStatusCode() {
return statusCode;
}

public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}

public String getExecuteTime() {
return executeTime;
}

public void setExecuteTime(String executeTime) {
this.executeTime = executeTime;
}

public String getAvailable() {
return available;
}

public void setAvailable(String available) {
this.available = available;
}

@Override
public String toString() {
return "Header [statusCode=" + statusCode + ", executeTime="
+ executeTime + ", available=" + available + "]";
}

}
<小时/>
package org.lyrics.xml.binding.track;

import org.simpleframework.xml.Element;

public class Body {

@Element(name = "track_list", required = false)
public TrackList trackList;

public TrackList getTrackList() {
return trackList;
}

public void setTrackList(TrackList trackList) {
this.trackList = trackList;
}

@Override
public String toString() {
return "Body [trackList=" + trackList + "]";
}

}
<小时/>
package org.lyrics.xml.binding.track;

import java.util.List;

import org.simpleframework.xml.ElementList;

public class TrackList {

@ElementList(name = "track", required = false)
public List<Track> tracks;

public List<Track> getTracks() {
return tracks;
}

public void setTracks(List<Track> tracks) {
this.tracks = tracks;
}

@Override
public String toString() {
return "TrackList [tracks=" + tracks + "]";
}

}
<小时/>
package org.lyrics.xml.binding.track;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "track")
public class Track {

@Element(name="track_id", required = false)
public String trackId;

@Element(name="track_mbid", required = false)
public String trackMbid;

@Element(name="track_length", required = false)
public String trackLength;

@Element(name="lyrics_id", required = false)
public String lyricsId;

@Element(name="instrumental", required = false)
public String instrumental;

@Element(name="subtitle_id", required = false)
public String subtitleId;

@Element(name="track_name", required = false)
public String trackName;

@Element(name="album_name", required = false)
public String albumName;

@Element(name="album_id", required = false)
public String albumId;

@Element(name="artist_id", required = false)
public String artistId;

@Element(name="album_coverart_100x100", required = false)
public String albumCoverart;

@Element(name="artist_mbid", required = false)
public String artistMbid;

@Element(name="artist_name", required = false)
public String artistName;

public String getTrackId() {
return trackId;
}

public void setTrackId(String trackId) {
this.trackId = trackId;
}

public String getTrackMbid() {
return trackMbid;
}

public void setTrackMbid(String trackMbid) {
this.trackMbid = trackMbid;
}

public String getTrackLength() {
return trackLength;
}

public void setTrackLength(String trackLength) {
this.trackLength = trackLength;
}

public String getLyricsId() {
return lyricsId;
}

public void setLyricsId(String lyricsId) {
this.lyricsId = lyricsId;
}

public String getInstrumental() {
return instrumental;
}

public void setInstrumental(String instrumental) {
this.instrumental = instrumental;
}

public String getSubtitleId() {
return subtitleId;
}

public void setSubtitleId(String subtitleId) {
this.subtitleId = subtitleId;
}

public String getTrackName() {
return trackName;
}

public void setTrackName(String trackName) {
this.trackName = trackName;
}

public String getAlbumName() {
return albumName;
}

public void setAlbumName(String albumName) {
this.albumName = albumName;
}

public String getAlbumId() {
return albumId;
}

public void setAlbumId(String albumId) {
this.albumId = albumId;
}

public String getArtistId() {
return artistId;
}

public void setArtistId(String artistId) {
this.artistId = artistId;
}

public String getAlbumCoverart() {
return albumCoverart;
}

public void setAlbumCoverart(String albumCoverart) {
this.albumCoverart = albumCoverart;
}

public String getArtistMbid() {
return artistMbid;
}

public void setArtistMbid(String artistMbid) {
this.artistMbid = artistMbid;
}

public String getArtistName() {
return artistName;
}

public void setArtistName(String artistName) {
this.artistName = artistName;
}

@Override
public String toString() {
return "Track [trackId=" + trackId + ", trackMbid=" + trackMbid
+ ", trackLength=" + trackLength + ", lyricsId=" + lyricsId
+ ", instrumental=" + instrumental + ", subtitleId="
+ subtitleId + ", trackName=" + trackName + ", albumName="
+ albumName + ", albumId=" + albumId + ", artistId=" + artistId
+ ", albumCoverart=" + albumCoverart + ", artistMbid="
+ artistMbid + ", artistName=" + artistName + "]";
}

}

最佳答案

我找到了解决方案。属性 inline=true 必须位于此处:

@ElementList(name = "track", required = false)
public List<Track> tracks;

关于java - Android XML 绑定(bind) - Simpleframework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6030640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com