我们在开发过程中可能遇到各种各样的问题,比如说实时通信中收不到消息等等。
下面会列举很多例子,看一下我们在各种情况里,是如何定位问题并解决的。

案例一

关键字:AVQuery、whereContaiedIn()、Arrays.asList()

问题详情

背景:_User 表有个 gender 字段,它的值是 0 或者 1。
需求:查询出 gender 字段包含 0 或者 1 的 _User 表数据,其实是 _User 表的所有数据。
尝试:使用了如下的代码

    AVQuery<AVObject> query = new AVQuery<>("_User");
    int[] genders = new int[]{0, 1};
    query.whereContainedIn("gender", Arrays.asList(genders));
    query.findInBackground(new FindCallback<AVObject>() {
        @Override
        public void done(List<AVObject> list, AVException e) {

        }
    });

问题:查不出任何结果。

定位问题

1、开启调试日志

首先在 Application 的 onCreate 中,开启调试日志,

AVOSCloud.setDebugLogEnabled(true);

2、找到复现代码所对应的 cURL 日志

===AVOS Cloud===: LogUtil$avlog->d->33: curl -X GET -H "X-LC-Id: I4ObRReguIe0oBzkM5UxmVx8" -H "X-LC-Key: YourAppKey" -G --data-urlencode 'where={"gender":{"$in":[[0,1]]}}' https://api.leancloud.cn/1.1/users

===AVOS Cloud===: LogUtil$avlog->d->33: {"results":[]}

4、参考相关 REST API 的 官方 cURL 日志12

curl -X GET \
  -H "X-LC-Id: BXq94xrYIGwsvjrLrvxvTOzw-gzGzoHsz" \
  -H "X-LC-Key: YourAppKey" \
  -H "Content-Type: application/json" \
  -G \
  --data-urlencode 'where={"upvotes":{"$in":[1,3,5,7,9]}}' \
  https://api.leancloud.cn/1.1/classes/Post

5、比对时发现了问题:我们的请求里多了一层的中括号 []

解决方案

分析到这里,我们明白原来是 Android 的代码写错了,需要把代码:

    int[] genders = new int[]{0, 1};
    query.whereContainedIn("gender", Arrays.asList(genders));

换成:

query.whereContainedIn("gender", Arrays.asList(0,1));
1 人赞了这个帖子.