如何在PHP中使用Android Jetpack Paging 3
我已经成功地在我的应用程序中实现了Android分页库, 问题是-我不知道如何将它与我的PHP代码一起作为后端使用,比如要加载的项目数等 get_feed.php如何在PHP中使用Android Jetpack Paging 3,php,android,android-studio,kotlin,pagination,Php,Android,Android Studio,Kotlin,Pagination,我已经成功地在我的应用程序中实现了Android分页库, 问题是-我不知道如何将它与我的PHP代码一起作为后端使用,比如要加载的项目数等 get_feed.php <?php $limit = 2; //find out how many rows are in the table $sql = "SELECT count(*) FROM uploads"; $result = $conn->prepare($sql); $result->execu
<?php
$limit = 2;
//find out how many rows are in the table
$sql = "SELECT count(*) FROM uploads";
$result = $conn->prepare($sql);
$result->execute();
$total = $result->fetchColumn();
//total pages
$totalpages = ceil($total/$limit);
//get the current page or set a default
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
// cast var as int
(int) $currentpage = $_GET['page'];
} else {
//default page num
$currentpage = 1;
}
//if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
}
//the offset of the list, based on current page
$offset = ($currentpage - 1) * $limit;
$stmt = "SELECT * FROM uploads ORDER by id DESC LIMIT $offset, $limit";
//I get all requiresd data, send and encode in json
echo json_encode($obj);
?>
FeedSource.kt
@GET(EndPoints.GET_RANDOM_FEED)
suspend fun getRandomFeed(
@Query("page") page: Int
) : PostsResponse
class FeedSource(private val apiDataSource: ApiDataSource) : PagingSource<Int, Posts>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Posts> {
return try {
//start from page 1 for undefined
val nextPage = params.key ?: 1
val response = apiDataSource.getRandomFeed(nextPage)
LoadResult.Page(
data = response.feeds,
prevKey = if (nextPage == 1) null else nextPage - 1,
nextKey = nextPage.plus(1)
)
} catch (e: Exception){
LoadResult.Error(e)
}
}
}
@Singleton
class FeedViewModel constructor(private val apiDataSource: ApiDataSource) : ViewModel() {
//I want to load 10 items at first
val feeds = Pager(PagingConfig(pageSize = 10)) {
FeedSource(apiDataSource)
}.flow.cachedIn(viewModelScope)
}
实际上,所有项目都是一次加载的。我已经设置了loadState适配器,它应该只在项目加载或出错时显示微调器(视情况而定)。
但现在,微调器仅在加载所有项并抛出HTTP 429错误时显示
如何使应用程序最初加载10个项目,然后根据我的PHP代码一次加载2个项目