首页 > 解决方案 > GitHub API 将 avatar_url 返回为 null

问题描述

所以我的 Kotlin 应用程序使用 RetroFit 2。很多字段,如 avatar_url、followers_url 都返回 null。我使用的 api 是“https://api.github.com/users/{username}/received_events”

我的界面

interface RetroFitService {
@Headers("Authorization: {personal_auth_token}")
@GET("{username}/received_events")
fun getActivityList(): Call<MutableList<FeedPageModel>>}

我的 RetroFit 客户端对象

object RetroFitClient {
private var retrofit: Retrofit? = null

fun getClient(baseUrl: String): Retrofit {
    if (retrofit == null) {
        retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
    return retrofit!!
}

}

输出

I/System.out: [FeedPageModel(id=15199828665, type=ForkEvent, actor=Actor(id=56252312, login=saikun0293, displayLogin=null, gravatarID=null, url=https://api.github.com/users /saikun0293, avatarURL=null), repo=Repo(id=229973079, name=GDGVIT/Resource-Hub, url=https://api.github.com/repos/GDGVIT/Resource-Hub), payload=Payload(forkee =Forkee(id=339402130, nodeID=null, name=Resource-Hub, fullName=null, private=false, owner=Owner(login=saikun0293, id=56252312, nodeID=null, avatarURL=null, gravatarID=null, url =https://api.github.com/users/saikun0293,htmlURL=null,followersURL=null,followingURL=null,gistsURL=null,

如您所见,各种字段都返回 null,但在使用浏览器时,相同的字段会返回数据。

请帮帮我:)

编辑我的模型数据类

data class FeedPageModel(
val id: String,
val type: String,
val actor: Actor,
val repo: Repo,
val payload: Payload,
val public: Boolean,
val createdAt: String,
val org: Actor? = null)

data class Actor(
val id: Long,
val login: String,
val displayLogin: String? = null,
val gravatarID: String,
val url: String,
val avatarURL: String)

data class Payload(
val forkee: Forkee? = null,
val action: String? = null,
val ref: String? = null,
val refType: String? = null,
val masterBranch: String? = null,
val description: String? = null,
val pusherType: String? = null,
val pushID: Long? = null,
val size: Long? = null,
val distinctSize: Long? = null,
val head: String? = null,
val before: String? = null,
val commits: List<Commit>? = null)

...和其他数据类...

标签: androidkotlinretrofit2github-api

解决方案


检查 FeedPageModel 数据是否所有变量都具有相同的变量在 kotlin 数据类中具有与 api 相同的数据类型。由于某些变量类型转换抛出异常,变量给出 null


推荐阅读