【发布时间】: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