如何在 android 中调用过滤后的推特 STREAM API?

How to call filtered twitter STREAM API in android?

我想通过 twitter 流 api 连续流式传输 twitter 推文,并进行改造且没有任何第 3 方库。当我尝试调用 api“https://api.twitter.com/2/tweets/search/stream”时,我只是第一次得到结果。如何直播?

我终于用这个片段实现了它

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)

}