简体   繁体   English

Json 占位符 API - 获取特定用户的照片

[英]Json Placeholder API - fetching photos for specific user

I'm having a little problem with filter in my android app because I'm using JsonPlaceholderApi , and I want to fetch photos for specific user and the problem is that you first need to fetch albums for user and then somewhat filter those photos which album id's we get with the request for albums.我的 android 应用程序中的过滤器有一点问题,因为我使用的是JsonPlaceholderApi ,我想为特定用户获取照片,问题是您首先需要为用户获取相册,然后对这些照片进行过滤id 是我们通过专辑请求获得的。 I managed to get the photos for first album but I have problem getting rest here is the code right now:我设法获得了第一张专辑的照片,但我在获取 rest 时遇到了问题,这里是现在的代码:

        viewModelScope.launch {
            var getAlbums = PostsApi.photosService.getUserAlbumsAsyncWithId(userId)
            var getPhotos = PostsApi.photosService.getAllPhotosAsync()

            try {
                val albumsForUser= getAlbums.await()
                Log.d("ImgRequest-albums", albumsForUser.toString())
                val photosResult = getPhotos.await()
                    .filter { list -> list.albumId == albumsForUser[1].id }
                Log.d("ImgRequest-photos", photosResult.toString())
                _selectedUserPhotos.value = photosResult
            } catch (e: Exception) {
                _selectedUserPhotos.value = ArrayList()
            }
        }


    }

How to filter more of them, is there any stream option I don't know about?如何过滤更多,有没有我不知道的 stream 选项? Or maybe make some dynamic request to the api?或者也许向 api 提出一些动态请求?

Answer I figure out is to change the filter to:我想的答案是将过滤器更改为:

.filter { list -> list.albumId in albumsForUser[0].id..albumsForUser[albumsForUser.size-1].id}

Don't know if it's 100% good solution but it works:)不知道这是否是 100% 好的解决方案,但它有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM