我正在与Strongloop / Loopback一起开发Android应用程序。我已将数据存储在MySQL数据库中,并且使用Strongloop / Loopback进行映射时没有问题。但是,使用Strongloop / Loopback从数据库中检索值时,列表始终返回一个大小但值为null的列表。无法弄清楚出了什么问题。任何人都可以帮我吗?非常感谢:))
这是我从Strongloop访问时对数据库的json响应:
[
{
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_1.png",
"rewards_equivalent_points": "10",
"id": 1 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_2.png",
"rewards_equivalent_points": "20",
"id": 2 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_3.png",
"rewards_equivalent_points": "30",
"id": 3 }, {
"rewards_image_name": "http://x.x.x.x/projects/images/rewards_4.png",
"rewards_equivalent_points": "40",
"id": 4 }
]
以下是获取列表的代码:
StrongloopClient strongloopClient = new StrongloopClient(this.context);
RestAdapter adapter = strongloopClient.getLoopBackAdapter("Rewards", "GET");
StrongloopRewardsModelRepository strongloopRewardsModelRepository = adapter.createRepository(StrongloopRewardsModelRepository.class);
strongloopRewardsModelRepository.findAll(new ListCallback<StrongloopRewardsModel>() {
@Override
public void onSuccess(List<StrongloopRewardsModel> arg0) {
Log.e("", "GetAllRewards success: " + arg0.get(0).getEquivalentPoints());
}
@Override
public void onError(Throwable arg0) {
Log.e("", "GetAllRewards error: " + arg0);
}
});
这是StrongloopClient:
public class StrongloopClient {
private Context context;
private RestAdapter adapter;
public StrongloopClient(Context contxt) {
context = contxt;
}
public RestAdapter getLoopBackAdapter(String transaction, String operation) {
if (adapter == null) {
adapter = new RestAdapter(context, "http://192.168.44.101:3000/api");
adapter.getContract().addItem(
new RestContractItem("/" + transaction, operation),
transaction);
}
return adapter;
}
以下是存储库的代码:
public class StrongloopRewardsModelRepository extends ModelRepository<StrongloopRewardsModel>{
public StrongloopRewardsModelRepository() {
super("Rewards", "Rewards", StrongloopRewardsModel.class);
}
}
这是模特:
public class StrongloopRewardsModel extends Model {
private Integer rewardsImageId;
private String rewardImageName;
private String equivalentPoints;
public Integer getRewardsImageId() {
return rewardsImageId;
}
public void setRewardsImageId(Integer rewardsImageId) {
this.rewardsImageId = rewardsImageId;
}
public String getRewardImageName() {
return rewardImageName;
}
public void setRewardImageName(String rewardImageName) {
this.rewardImageName = rewardImageName;
}
public String getEquivalentPoints() {
return equivalentPoints;
}
public void setEquivalentPoints(String equivalentPoints) {
this.equivalentPoints = equivalentPoints;
}
}
答案 0 :(得分:1)
最后,我发现了什么是错的。 POJO应该与models.json中创建的模型的字段匹配。谢谢你的时间。
但是,我的另一个问题是,当我使用过滤器进行查询时,例如将过滤器作为传递给Strongloop适配器的“参数”的一部分传递,它似乎返回所有模型实例而不是过滤的模型实例。我的问题中的代码相同,只是getLoopbackAdapter(“Rewards”,“GET”)变为“Rewards?filter [where] [rewards_equivalent_points] = 10”,“GET”)。任何想法为什么它表现如此?谢谢:))
答案 1 :(得分:0)
这是因为您的搜索没有找到任何实例。我不确定您是否需要在您的应用上处理此问题,或者SDK是否需要处理此问题。
例如,当尝试通过rest接口按元素[1]进行搜索时,它会转换为404(未找到):rest:{after:convertNullToNotFoundError}
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L771