RestSharp consuming SubSonic REST Api

本文关键字:Api REST SubSonic consuming RestSharp | 更新日期: 2023-09-27 18:21:39

我目前正在编写一个c#库,以便在windows、iOS和Android上使用跨平台。我正在使用Rest服务,并且在从响应返回对象时遇到一些问题。我正在使用RestSharp进行api调用。我使用Xsd2Code.NET从api提供的xsd生成我的类。

问题是响应被封装在<subsonic-response>中。我想要的项目包含在中。如果我将类型作为List<NowPlaying>传入,但其中的项没有填充到NowPlaying对象中,RestSharp将尝试解析并执行此操作。我为NowPlaying生成了序列化/反序列化方法,但由于<subsonic-response>是根元素,因此引发了异常。有没有办法删除<subsonic-response>?我试着回应。RootElement=RestSharp呼叫的"亚音速响应",但不起作用。请参阅下面的回复。任何帮助都会很棒。

RestResponse:

<?xml version="1.0" encoding="UTF-8"?>
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.7.0">
<nowPlaying>
    <entry     id="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c38372e2044616e63652044616e63652028445542535445502052454d495829202d20426967205365616e2e6d7033" parent="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b3132" 
title="Dance Dance (DUBSTEP REMIX) - Big Sean" 
isDir="false" 
album="M3 MIXTAPE (MEMBA. ME. MAAD)" 
artist="DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE"
duration="67"
bitRate="192"
year="2012"
size="1615419" 
suffix="mp3" 
contentType="audio/mpeg" 
isVideo="false"
coverArt="503a5c4d757369635c4a616e20326b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d3320284d454d4241204d45204d4141442920324b31325c444a20434852495354554646204449204d414420595554452046524f4d2052454e41495353414e43452050524553454e5453204d332020284d454d4241204d45204d4141442920324b31325c444a20434852495354554646202d204d454d4241204d45204d4141442046524f4e542e6a7067"
 path="Jan 2k12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3 (MEMBA ME MAAD) 2K12/DJ CHRISTUFF DI MAD YUTE FROM RENAISSANCE PRESENTS M3  (MEMBA ME MAAD) 2K12/87. Dance Dance (DUBSTEP REMIX) - Big Sean.mp3" 
username="admin" 
playerId="2" 
playerName="subAir"
minutesAgo="0"/>
 </nowPlaying>
</subsonic-response>

生成的类:

public partial class NowPlaying : EntityBase<NowPlaying>
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    private List<NowPlayingEntry> entryField;
    public List<NowPlayingEntry> entry
    {
        get
        {
            if ((this.entryField == null))
            {
                this.entryField = new List<NowPlayingEntry>();
            }
            return this.entryField;
        }
        set
        {
            if ((this.entryField != null))
            {
                if ((entryField.Equals(value) != true))
                {
                    this.entryField = value;
                    this.OnPropertyChanged("entry");
                }
            }
            else
            {
                this.entryField = value;
                this.OnPropertyChanged("entry");
            }
        }
    }
}

我调用的方法是从休息服务获得NowPlay

    public NowPlaying getNowPlaying()
    {
        NowPlaying playing;
        try
        {
            var request = new RestRequest();
            request.Resource = "getNowPlaying.view";
            playing = SendRequest<NowPlaying>(request);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return playing;
    }

RestSharp consuming SubSonic REST Api

解决了我的问题。最终的结果是不让RestSharp反序列化我的对象。获取作为xml响应的RestResponse内容。然后反序列化api的Response对象。抓取response.item并将其投射到我的NowPlaying对象中,所有数据都在对象中。

string xml = SendRequest(request);
var res = Response.Deserialize(xml);
playing =(NowPlaying)res.Item;

与该模式匹配的C#类的一般结构是:

public class SubsonicResource {
    public List<entry> NowPlaying { get; set; }
}
public class entry {
    public string Id { get; set; }
    public string Path { get; set; }
    public string Username { get; set; }
    ... 
}

然后您可以调用Execute<SubsonicResource>(),它应该被填充。