使用 xamarin with realm 从服务器获取数据
fetch data from server using xamarin with realm
我正在尝试使用 Xamarin
和 Realm
创建移动应用程序。我正在尝试从服务器中提取数据并将其加载到由 RealmObjects 扩展的对象中。
从服务器获取数据的代码
public async void getCompanyMaster() {
var uri = new Uri(string.Format(URL, string.Empty));
try
{
getConnection();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Company company = JsonConvert.DeserializeObject<Company>(content);
Debug.WriteLine("Company List " );
}
}
catch (Exception e)
{
Debug.WriteLine("Error {0}", e.Message);
}
}
境界模型公司
namespace DesignModel.Model.Master
{
public class Company : RealmObject
{
public string errorMessage { get; set; }
public string status { get; set; }
[JsonProperty(PropertyName = "companyMaster")]
IList<CompanyMaster> companyMaster { get; }
}
}
公司主人的领域模型
namespace DesignModel.Model.Master
{
public class CompanyMaster : RealmObject
{
public string companyAddress { get; set; }
public string companyId { get; set; }
public string companyName { get; set; }
public string companyPhoneNumber { get; set; }
}
}
现在问题出在 getCompanyMaster
函数中的下一行
Company company = JsonConvert.DeserializeObject<Company>(content);
它引发了一个异常
Error Error setting value to 'errorMessage' on
'DesignModel.Model.Master.Company'.
如果我不从 RealmObject
扩展我的 class 那么它工作正常但是当我添加 RealObject
它不起作用。
上述方法在 android 本机中有效,但不知道为什么它在 xamarin 中不起作用。
样本json
{
"errorMessage": "",
"status": "Ok",
"companyMaster": [
{
"companyAddress": "123 Coffee Street\nSuite 300\nRedmond, WA 98052\nUSA",
"companyId": "cec",
"companyName": "Contoso Entertainment Systems",
"companyPhoneNumber": "425-555-0156"
}]
}
完整的堆栈跟踪
[0:] Error Newtonsoft.Json.JsonSerializationException: Error setting
value to 'errorMessage' on 'DesignModel.Model.Master.Company'. --->
System.PlatformNotSupportedException: The PCL build of Realm is being
linked which probably means you need to use NuGet or otherwise link a
platform-specific Realm.dll to your main application.
从此处找到的文档中添加注释
https://realm.io/docs/xamarin/latest/
在Getting Started - Prerequisites
之下
Important note for PCL users - this works via the NuGet Bait and Switch
Trick. You
have to install the Realm NuGet package into every PCL that uses Realm
as well as each of your platform-specific projects, e.g.: your final
app for iOS and Android. If you are using shared projects, you only
have to install the NuGet into each platform-specific project.
我正在尝试使用 Xamarin
和 Realm
创建移动应用程序。我正在尝试从服务器中提取数据并将其加载到由 RealmObjects 扩展的对象中。
从服务器获取数据的代码
public async void getCompanyMaster() {
var uri = new Uri(string.Format(URL, string.Empty));
try
{
getConnection();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Company company = JsonConvert.DeserializeObject<Company>(content);
Debug.WriteLine("Company List " );
}
}
catch (Exception e)
{
Debug.WriteLine("Error {0}", e.Message);
}
}
境界模型公司
namespace DesignModel.Model.Master
{
public class Company : RealmObject
{
public string errorMessage { get; set; }
public string status { get; set; }
[JsonProperty(PropertyName = "companyMaster")]
IList<CompanyMaster> companyMaster { get; }
}
}
公司主人的领域模型
namespace DesignModel.Model.Master
{
public class CompanyMaster : RealmObject
{
public string companyAddress { get; set; }
public string companyId { get; set; }
public string companyName { get; set; }
public string companyPhoneNumber { get; set; }
}
}
现在问题出在 getCompanyMaster
函数中的下一行
Company company = JsonConvert.DeserializeObject<Company>(content);
它引发了一个异常
Error Error setting value to 'errorMessage' on 'DesignModel.Model.Master.Company'.
如果我不从 RealmObject
扩展我的 class 那么它工作正常但是当我添加 RealObject
它不起作用。
上述方法在 android 本机中有效,但不知道为什么它在 xamarin 中不起作用。
样本json
{
"errorMessage": "",
"status": "Ok",
"companyMaster": [
{
"companyAddress": "123 Coffee Street\nSuite 300\nRedmond, WA 98052\nUSA",
"companyId": "cec",
"companyName": "Contoso Entertainment Systems",
"companyPhoneNumber": "425-555-0156"
}]
}
完整的堆栈跟踪
[0:] Error Newtonsoft.Json.JsonSerializationException: Error setting value to 'errorMessage' on 'DesignModel.Model.Master.Company'. ---> System.PlatformNotSupportedException: The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.
从此处找到的文档中添加注释
https://realm.io/docs/xamarin/latest/
在Getting Started - Prerequisites
之下Important note for PCL users - this works via the NuGet Bait and Switch Trick. You have to install the Realm NuGet package into every PCL that uses Realm as well as each of your platform-specific projects, e.g.: your final app for iOS and Android. If you are using shared projects, you only have to install the NuGet into each platform-specific project.