小编Phi*_*lly的帖子

如何使用 Kotlin 协程在 Retrofit 中处理 204 响应?

我正在使用带有 Kotlin 协程的 Retrofit 2.7.1。

我有一个 Retrofit 服务定义如下:

@PUT("/users/{userId}.json")
suspend fun updateUserProfile(
        @Path("userId") userId: String,
        @Query("display_name") displayName: String) : Void
Run Code Online (Sandbox Code Playgroud)

此调用返回HTTP 204 No Content响应,这会导致 Retrofit 崩溃:

kotlin.KotlinNullPointerException: Response from com.philsoft.test.api.UserApiService.updateUserProfile was null but response body type was declared as non-null
        at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:43)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
Run Code Online (Sandbox Code Playgroud)

如何在改造中使用协程处理 204 响应而不会崩溃?

android retrofit retrofit2 kotlin-coroutines

18
推荐指数
4
解决办法
7238
查看次数

在 Android 上启动协程的正确方法是什么?

我想弄清楚如何启动协程。我希望它按顺序调用两个挂起函数。

我阅读的第一个文档说要这样做:

class Something {
  fun initialize() {
    launch {
      suspendFun1()
      suspendFun2()
    }
}

Run Code Online (Sandbox Code Playgroud)

但是launchAndroid Studio没有找到该方法。然后我了解到官方协程文档建议使用GlobalScope.launch

class Something {
  fun initialize() {
    GlobalScope.launch {
      suspendFun1()
      suspendFun2()
    }
}
Run Code Online (Sandbox Code Playgroud)

但后来我在这篇文章中读到,你不应该使用GlobalScope.launch.

所以我找到了另一篇博文,解释说我需要一个 CoroutineScope 来调用launch. 但它没有解释如何构建一个。

然后我找到了这篇博客文章,解释了如何通过在类上实现 CoroutineScope 来为活动和视图模型构建一个:

class Something : CoroutineScope {
  private lateinit var job: Job
  override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.Main

  fun initialize() {
    job = Job()
    launch {
      suspendFun1() …
Run Code Online (Sandbox Code Playgroud)

android kotlin-coroutines

13
推荐指数
3
解决办法
4752
查看次数

VSCode:是否有一个选项可以为 Typescript 生成尚不存在的函数?

我希望能够调用尚不存在的函数名称,然后能够使用快速操作使其生成具有该名称的函数。例如,如果我输入以下内容,则使用 TypeScript:

const name: string = 'Jon'
display(name)
Run Code Online (Sandbox Code Playgroud)

我会收到一个错误,display(name)因为Cannot find name: display它还不存在。

我想要的是自动生成此函数的快速操作,以便它会为我写入:

function display(name: string) {
  //TODO
}
Run Code Online (Sandbox Code Playgroud)

有这个插件或者什么的吗?我对 VSCode 比较陌生。我来自android开发,Android Studio有这个功能,它节省了我很多时间。

typescript visual-studio-code

8
推荐指数
0
解决办法
847
查看次数

如何刷新 RecyclerView + PagedListAdapter 并滚动到顶部?

我有一个RecyclerView正在使用PagedListAdapter. 我想刷新列表(即从 my 中获取数据DataSource)并自动滚动到顶部。

这是我的 RecyclerView:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
    val adapter by lazy {
        DiscoveryListAdapter(activity)
    }
Run Code Online (Sandbox Code Playgroud)
        recyclerView.let {
            it.adapter = adapter
            it.layoutManager = LinearLayoutManager(activity)
        }
Run Code Online (Sandbox Code Playgroud)

这是我的适配器:

class DiscoveryListAdapter() : PagedListAdapter<Discovery, DiscoveryListItemViewHolder>(DiscoveryDiffCallback()) {

    @SuppressLint("CheckResult")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DiscoveryListItemViewHolder {
        //Extension function
        val view = parent.inflate(R.layout.discovery_detail_list_item)
        return DiscoveryListItemViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: DiscoveryListItemViewHolder, position: Int) {
        getItem(position)?.let {
            viewHolder.populateFrom(it)
        }
    }

    class DiscoveryDiffCallback : DiffUtil.ItemCallback<Discovery>() {
        override fun areItemsTheSame(one: Discovery, two: …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-paging

6
推荐指数
1
解决办法
1629
查看次数

如何知道 Android 的 EditText 中发生了删除操作?

当一个字符在 EditText 中被删除时,我想得到一个回调。

我该怎么做?

android textwatcher android-edittext

5
推荐指数
2
解决办法
1046
查看次数

在 Kotlin 中使用 @Parcelize 注释时如何忽略字段

我想@Parcelize在 Kotlin 中使用注解时忽略一个字段,这样该字段就不会被打包,因为这个字段没有实现Parcelable接口。

从这个开始,我们得到一个错误,因为它PagedList是不可分割的:

@Parcelize
data class LeaderboardState(
    val progressShown: Boolean = true,
    val pagedList: PagedList<QUser>? = null
) : Parcelable
Run Code Online (Sandbox Code Playgroud)

给出:

Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'
Run Code Online (Sandbox Code Playgroud)

标记为@Transient给出与上述相同的错误:

@Parcelize
data class LeaderboardState(
    val progressShown: Boolean = true,

    //Same error
    @Transient
    val pagedList: PagedList<QUser>? = null
) : Parcelable
Run Code Online (Sandbox Code Playgroud)

我发现有一个未记录的注释被调用@IgnoredOnParcel,它给出了相同的错误,注释上有一个 lint 错误:

@Parcelize
data …
Run Code Online (Sandbox Code Playgroud)

android parcelable kotlin

5
推荐指数
1
解决办法
2680
查看次数