1

我正在结合 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;
}
}
4

2 回答 2

1

最后,我发现了问题所在。POJO 应该与在 models.json 中创建的模型的字段相匹配。谢谢你的时间。

但是,我的另一个问题是,当我使用过滤器进行查询时,例如将过滤器作为“参数”的一部分传递给 Strongloop 适配器,它似乎返回所有模型实例而不是过滤的实例。与我的问题相同的代码,只是 getLoopbackAdapter("Rewards","GET") 变为 "Rewards?filter[where][rewards_equivalent_points]=10","GET")。任何想法为什么它会这样?谢谢 :)

于 2014-06-19T10:31:08.670 回答
0

这是因为您的搜索没有找到任何实例。我不确定您是否需要在您的应用程序上处理此问题,或者 SDK 是否处理此问题。

例如,当尝试通过 rest 接口按元素 [1] 搜索时,它会转换为 404(未找到):rest: {after: convertNullToNotFoundError}

https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L771

于 2014-06-20T17:36:29.197 回答