Async Vala Example

问题描述 投票:0回答:1

在[[Michael Lauer]博士所著的《对Vala的介绍》中,>,他提到lib Soup async api已损坏。我正在努力使用session.queue_message编写一个简单的示例,该示例使用radio-browser中的服务查询广播电台。这是我的代码。如果有经验的程序员(例如“ Al Thomas”)提供任何帮助,我将深表感谢。谢谢。public class Station : Object { // A globally unique identifier for the change of the station information public string changeuuid { get; set; default = ""; } // A globally unique identifier for the station public string stationuuid { get; set; default = ""; } // The name of the station public string name { get; set; default = ""; } // The stream URL provided by the user public string url { get; set; default = ""; } // and so on ... many properties public string to_string () { var builder = new StringBuilder (); builder.append_printf ("\nchangeuuid = %s\n", changeuuid); builder.append_printf ("stationuuid = %s\n", stationuuid); builder.append_printf ("name = %s\n", name); builder.append_printf ("url = %s\n", url); return (owned) builder.str; } } public class RadioBrowser : Object { private static Soup.Session session; // private static MainLoop main_loop; public const string API_URL = "https://de1.api.radio-browser.info/json/stations"; public const string USER_AGENT = "github.aeldemery.radiolibrary"; public RadioBrowser (string user_agent = USER_AGENT, uint timeout = 50) requires (timeout > 0) { Intl.setlocale (); session = new Soup.Session (); session.timeout = timeout; session.user_agent = user_agent; session.use_thread_context = true; // main_loop = new MainLoop (); } private void check_response_status (Soup.Message msg) { if (msg.status_code != 200) { var str = "Error: Status message error %s.".printf (msg.reason_phrase); error (str); } } public Gee.ArrayList<Station> listStations () { var stations = new Gee.ArrayList<Station> (); var data_list = Datalist<string> (); data_list.set_data ("limit", "100"); var parser = new Json.Parser (); parser.array_element.connect ((pars, array, index) => { var station = Json.gobject_deserialize (typeof (Station), array.get_element (index)) as Station; assert_nonnull (station); stations.add (station); }); var msg = Soup.Form.request_new_from_datalist ( "POST", API_URL, data_list ); // send_message works but not queue_message // session.send_message (msg); session.queue_message (msg, (sess, mess) => { check_response_status (msg); try { parser.load_from_data ((string) msg.response_body.flatten ().data); } catch (Error e) { error ("Failed to parse data, error:" + e.message); } }); return stations; } } int main (string[] args) { var radio_browser = new RadioBrowser (); var stations = radio_browser.listStations (); assert_nonnull (stations); foreach (var station in stations) { print (station.to_string ()); } return 0; }

[在Michael Lauer博士的《 Vala简介》中,他提到lib Soup异步api已损坏。我正在努力使用session.queue_message编写一个简单的示例来查询广播...
asynchronous vala libsoup
1个回答
0
投票
虽然我不是Al Thomas,但我仍然可以提供帮助。 ;)
© www.soinside.com 2019 - 2024. All rights reserved.