首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在android中调用过滤过的twitter流API?

如何在android中调用过滤过的twitter流API?
EN

Stack Overflow用户
提问于 2021-08-20 18:32:35
回答 1查看 90关注 0票数 1

我想与推特流api与改进和没有任何第三方库的推特推文连续流。当我尝试调用“https://api.twitter.com/2/tweets/search/stream”接口时,我只是第一次得到结果。如何流媒体?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-09 06:00:16

我最终使用以下代码片段实现了这一点

代码语言:javascript
运行
复制
override suspend fun streamTweets(): Flow<Resource<TweetResponseModel>> {
        return flow {
            val client: OkHttpClient = OkHttpClient().newBuilder().addInterceptor(
                BasicAuthInterceptor(getAPIKey(), getAPISecretKey())
            ).build()

        val request: Request = Request.Builder()
            .url(TWITTER_STREAM_URL)
            .method("GET", null)
            .build()
        val response: okhttp3.Response = client.newCall(request).execute()
        val source = response.body?.source()
        val buffer = Buffer()
        while (!source!!.exhausted()) {
            response.body?.source()?.read(buffer, 8192)
            val data = buffer.readString(Charset.defaultCharset())
            try {
                val tweetResponseModel: TweetResponseModel =
                    Gson().fromJson(data, TweetResponseModel::class.java)
                emit(Resource.Success(tweetResponseModel))
            } catch (e: Exception) {
                Log.e("jsonException", data)
            }
        }
    }.flowOn(ioDispatcher)

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68866693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档