如何在没有查询的情况下在 Retrofit 中提取 Json 数据

Posted

技术标签:

【中文标题】如何在没有查询的情况下在 Retrofit 中提取 Json 数据【英文标题】:how to extract Json data without having a query in Retrofit 【发布时间】:2021-08-15 08:17:01 【问题描述】:

当我有一个 Api Key 时,我使用以下代码从中提取 Json 数据。

现在我想从 https://api.coingecko.com/api/v3/exchanges 获取 Json 数据,但我没有任何 Api Key 或查询要传递。如何使用 RetroFit 来完成?

import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.create
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Query

const val BASE_URL = "https://newsapi.org/"
const val API_KEY = "5f60ae62gcbc4bdaa0d15164d7f1275b"

interface NewsInterface 
    @GET("v2/top-headlines?apiKey=$API_KEY")
    fun getHeadLines(@Query("country")country:String): Call<News>



object NewsService 
    val newsInstance :NewsInterface

    init 
        val retrofit: Retrofit= Retrofit.Builder()
            .baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()

        newsInstance = retrofit.create(NewsInterface::class.java)


    

【问题讨论】:

【参考方案1】:

你的 API 乐趣应该是,

@GET("api/v3/exchanges")
fun getExchanges(): Call<Response>

【讨论】:

以上是关于如何在没有查询的情况下在 Retrofit 中提取 Json 数据的主要内容,如果未能解决你的问题,请参考以下文章