我正在与Strongloop / Loopback一起开发Android应用程序。我已经将数据存储在MySQL数据库中,并且可以使用Strongloop / Loopback映射它。但是,当使用Strongloop / Loopback从数据库中检索值时,列表始终返回一个大小,但具有空值。无法弄清楚出什么问题了。有人可以帮我吗?非常感谢 :)
这是从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;
}
}
最佳答案
最后,我发现了问题所在。 POJO应匹配在models.json中创建的模型的字段。谢谢你的时间。
但是,我的另一个问题是,当我使用过滤器进行查询时,例如将过滤器作为传递给Strongloop适配器的“参数”的一部分传递时,似乎返回了所有模型实例,而不是过滤的实例。与我的问题相同的代码,只是getLoopbackAdapter(“ Rewards”,“ GET”)变为“ Rewards?filter [where] [rewards_equivalent_points] = 10”,“ GET”)。有什么想法为什么会这样吗?谢谢 :)
关于android - Strongloop/Loopback findAll功能返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24256461/