我是libRETS的新手。我想从MidWEST RETS服务中提取一些MLS数据。我按照网站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数据。请注意,我已经附加了一个记录器代表。我很无能为什么我的populate函数失败,尽管服务器返回了查询结果。任何形式的帮助将不胜感激。
答案 0 :(得分:1)
您可能需要配置搜索格式类型。我知道FlexMLS要求使用COMPACT_DECODED,否则hasNext()将返回false。
searchRequest.SetFormatType(SearchRequest.FormatType.COMPACT_DECODED);