【问题标题】:Handle HttpException in Paging3 Android在 Paging3 Android 中处理 HttpException
【发布时间】:2021-04-08 15:27:11
【问题描述】:

我有一个这样的PagingSource

class NotificationPagingSource(
    private val tayehAPI: TayehAPI,
    private val token: String,
) : PagingSource<Int, Notification>() {
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Notification> {
        val nextPageNumber = params.key ?: STARTING_PAGE_INDEX

        return try {
            val response = tayehAPI.getNotifications(
                tokenString = token,
                page = nextPageNumber,
                perPage = params.loadSize
            )
            val notifications = response.objects

            LoadResult.Page(
                data = notifications,
                prevKey = if (nextPageNumber == STARTING_PAGE_INDEX) null else nextPageNumber - 1,
                nextKey = if (notifications.isEmpty()) null else nextPageNumber + 1
            )
        } catch (e: IOException) {
            LoadResult.Error(e)
        } catch (e: HttpException) {
            LoadResult.Error(e)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, Notification>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }
}

我想处理 HttpExceptions 之类的 403 或 404。我该如何实现?

【问题讨论】:

    标签: android android-paging


    【解决方案1】:

    您需要像这样将addLoadStateListener 添加到您的分页适配器:

    notificationAdapter.addLoadStateListener { loadState ->
                val errorState = when {
                    loadState.append is LoadState.Error -> loadState.append as LoadState.Error
                    loadState.prepend is LoadState.Error ->  loadState.prepend as LoadState.Error
                    loadState.refresh is LoadState.Error -> loadState.refresh as LoadState.Error
                    else -> null
                }
                errorState?.let {
                    if (errorState.error.localizedMessage == "HTTP 403 Forbidden") {
                        AlertDialog.Builder(requireContext())
                            .setMessage(R.string.token_expire)
                            .setPositiveButton("Ok") { _, _ ->
                                findNavController().navigate(
                                    NotificationFragmentDirections.actionGlobalHomeFragment()
                                )
                            }
                            .setCancelable(false)
                            .create()
                            .show()
    
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      我在Kaaveh's answer 的基础上提出了这个改进的解决方案。 我认为处理 HTTP 代码而不是错误消息文本会更好。

      notificationAdapter.addLoadStateListener { loadState ->
          val errorState = when {
              loadState.prepend is LoadState.Error -> loadState.prepend as LoadState.Error
              loadState.append is LoadState.Error -> loadState.append as LoadState.Error
              loadState.refresh is LoadState.Error -> loadState.refresh as LoadState.Error
              else -> null
          }
      
          when (val throwable = errorState?.error) {
              is IOException -> { /* Handle IO exceptions */ }
              is HttpException -> {
                  if (throwable.code() == 401) { /* Handle HTTP 401 */ }
                  else { /* Do something else */ }
              }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 2022-10-06
        相关资源
        最近更新 更多