简体   繁体   English

使用RestSharp的SubSonic REST Api

[英]RestSharp consuming SubSonic REST Api

I am currently writing ac# library to use cross platform on windows, iOS, Android. 我目前正在编写ac#库,以在Windows,iOS,Android上使用跨平台。 I am consuming a Rest service and having some trouble with the objects being returned from the response. 我正在使用Rest服务,并且从响应返回的对象有些麻烦。 I am using RestSharp for the api calls. 我正在使用RestSharp进行api调用。 I used Xsd2Code.NET to generate my classes from an xsd provided by the api. 我使用Xsd2Code.NET从api提供的xsd生成类。

Problem is the responses are wrapping in a <subsonic-response> . 问题在于响应包装在<subsonic-response> The item I want is contained within. 我想要的项目包含在其中。 RestSharp tries to parse and does if I pass in the type as a List<NowPlaying> but the items within that do not get populated to the NowPlaying object. RestSharp尝试解析,并且如果我将类型作为List<NowPlaying>传入时执行,但是其中的项目不会填充到NowPlaying对象中。 I generated the serialize/deserialize methods for NowPlaying but due to the <subsonic-response> as the root element an exception is thrown. 我为NowPlaying生成了序列化/反序列化方法,但是由于<subsonic-response>作为根元素,因此引发了异常。 Is there a way to remove <subsonic-response> ? 有没有办法删除<subsonic-response> I tried response.RootElement = "subsonic-response" for the RestSharp call but does not work. 我尝试了RestSharp调用的response.RootElement =“ subsonic-response”,但不起作用。 See response below. 请参阅下面的响应。 Any help would be great. 任何帮助都会很棒。

RestResponse: 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>

A class that was generated: 生成的类:

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");
            }
        }
    }
}

My method I am calling to get the NowPlaying from the rest service 我正在调用的方法是从其余服务中获取NowPlaying

    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;

    }

Solved my problem. 解决了我的问题。 End result was to not let RestSharp deserialize my object. 最终结果是不要让RestSharp反序列化我的对象。 Get the RestResponse content which is the the xml response. 获取RestResponse内容,它是xml响应。 Then deserialize the Response object for the api. 然后反序列化api的Response对象。 Grabbed the response.item and casted it to my NowPlaying object and all data was in object. 抓取response.item并将其转换为我的NowPlaying对象,所有数据都在对象中。

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

The general structure for a C# class that will match that schema is this: 与该模式匹配的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; }
    ... 
}

Then you can call Execute<SubsonicResource>() and it should be populated. 然后,您可以调用Execute<SubsonicResource>() ,并且应将其填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM