【问题标题】:how to extract Json data without having a query in Retrofit如何在没有查询的情况下在 Retrofit 中提取 Json 数据
【发布时间】: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)


    }
}

【问题讨论】:

    标签: android json android-studio kotlin mobile-development


    【解决方案1】:

    你的 API 乐趣应该是,

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

    【讨论】:

      猜你喜欢
      • 2021-02-06
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 2018-06-27
      • 2016-09-15
      • 1970-01-01
      相关资源
      最近更新 更多