0

我对 libRETS 很陌生。我想从 MidWEST RETS 服务中提取一些 MLS 数据。我按照 web RESO 官方网站中提到的示例进行操作。http://www.reso.org/assets/Resources/CodeExamples/c-sharp-connection-ex.txt。我使用 C# 作为编程语言并使用 nuget 包https://www.nuget.org/packages/libRETS-x64/。我可以登录,但发送查询后,我没有得到结果。我在下面粘贴我的示例。

using librets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibRETSExample
{
    public class RetsHelper : IDisposable
    {
        private List<Listing> listings = new List<Listing>();

        #region Properties

        /// <summary>
        /// Contains the rets session connection.
        /// </summary>
        internal RetsSession Session
        {
            get
            {
                return _sess;
            }
            set
            {
                _sess = value;
            }
        }
        private RetsSession _sess;

        #endregion


        #region Constructors

        public RetsHelper(string url, string userAgent, string userName,
            string password)
        {
            this.Session = new RetsSession(url);
            //this.Session.SetUserAgent(userAgent);
            this.Session.LoggerDelegate = new RetsHttpLogger.Delegate(LogRETS);
            //this.Session.SetDefaultEncoding(EncodingType.RETS_XML_ISO_ENCODING);
            try
            {   //Log in to RETS
                bool loginResult = this.Session.Login(userName, password);

                if (!loginResult)
                {
                    Trace.WriteLine("\nLogin to RETS Failed at " + DateTime.Now);
                }
                else
                {
                    Trace.WriteLine("\nLogin to RETS Succeeded at " + DateTime.Now);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        #endregion

        #region Instance Methods

        /// <summary>
        /// Gets search results for a given query.
        /// </summary>
        /// <param name="searchType">Resource from rets metadata</param>
        /// <param name="searchClass">Class from rets metadata</param>
        /// <param name="query">RETS query to run.</param>
        /// <param name="offset">Record number to start at.  This is 1-based, not zero-based.</param>
        /// <returns>Results of query.</returns>
        internal SearchResultSet GetRetsSearchResults(string searchType, string searchClass, 
            string query, int offset)
        {
            using (SearchRequest search = this.Session.CreateSearchRequest(
                searchType, searchClass.ToString(), query))
            {
                search.SetQueryType(SearchRequest.QueryType.DMQL2);
                search.SetStandardNames(false);
                search.SetLimit(10);
                //search.SetFormatType(SearchRequest.FormatType.COMPACT);
                search.SetOffset(offset);

                SearchResultSet results = this.Session.Search(search);
                return results;
            }
        }


        /// <summary>
        /// Downloads all listings, starting at the given offset.  This method will recurse if needed.
        /// </summary>
        /// <param name="propType"></param>
        /// <param name="offset">Starting record of results.  This is 1-based, not zero-based.</param>
        internal void DownloadAllListings(string propType, int offset)
        {
            try
            {
                using (SearchResultSet results = GetRetsSearchResults("Property", propType, "(LP=300000-)", offset))
                {
                    // get the results as a list of objects.
                    List<Listing> list = Populate(results);

                    // Add to the master list
                    listings.AddRange(list);              

                    // check to see if the list finished to the end.
                    int totalProcessed = list.Count + offset;
                    if (results.GetCount() > totalProcessed)
                    {
                        // recurse if needed.
                        DownloadAllListings(propType, totalProcessed);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        public List<Listing> GetAllListings(string propType, int offset)
        {
            listings.Clear();
            DownloadAllListings(propType, offset);
            return listings;
        }

        /// <summary>
        /// Populates a ListingCollection from a RETS result set.
        /// </summary>
        /// <param name="results">ResultSet to create the ListingCollection from.</param>
        /// <returns>ListingCollection representing the result set.  Will return 
        /// an empty collection if no records.</returns>
        private List<Listing> Populate(SearchResultSet results)
        {
            List<Listing> listings = new List<Listing>();

            while (results.HasNext())
            {
                Listing currentListing = new Listing();
                //sample db mapping
                currentListing.StreetNumber = results.GetString("StreetNumber");
                currentListing.StreetDirection = results.GetString("StreetDirection");
                currentListing.StreetName = results.GetString("StreetName");
                listings.Add(currentListing);
            }
            return listings;
        }

        #endregion

        #region IDisposable Members
        /// <summary>
        /// Logout of the RETS session.
        /// </summary>
        public void Dispose()
        {
            try
            {
                Session.Logout();
            }
            finally
            {
                Session.Dispose();
            }
        }

        #endregion

        public void LogRETS(RetsHttpLogger.Type type, byte[] data)
        {
            Trace.WriteLine(type.ToString() + ": " + Encoding.UTF8.GetString(data));
        }

    }

    public class Listing
    {
        public string StreetNumber { get; set; }

        public string StreetDirection { get; set; }

        public string StreetName { get; set; } 
    }
}

我面临的问题是在函数 private List Populate(SearchResultSet results) 内部,代码永远不会进入 while 循环,因为 HasNext() 总是返回 false。但实际上查询已发送到服务器,服务器返回结果。我可以在 Trace 窗口中将它们视为 xml 数据。请注意,我附上了一个记录器代表。尽管服务器返回了查询结果,但我有点不知道为什么我的填充函数失败了。任何形式的帮助将不胜感激。

4

1 回答 1

1

您可能需要配置搜索格式类型。我知道 FlexMLS 需要使用 COMPACT_DECODED,否则 hasNext() 将返回 false。

searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT_DECODED);
于 2016-03-03T14:36:44.103 回答