【发布时间】:2021-10-11 23:24:39
【问题描述】:
我一直在努力解决这个问题,我正在尝试使用此代码进行改造来调用 API
interface HTTPService {
@GET("/v1/breeds")
suspend fun getbreeds():BreedList
}
class Retrofitclass {
companion object{
val BaseURL = "https://docs.thedogapi.com"
fun getRetroInstance(): Retrofit {
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(BaseURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
}
}
在视图模型中:
fun makeApiCall(){
viewModelScope.launch(Dispatchers.IO){
// try {
val retroinstance = Retrofitclass.getRetroInstance().create(HTTPService::class.java)
val response = retroinstance.getbreeds()
print("responce"+ response )
selectdatalist.postValue(response)
// } catch (e: Exception) {
// print("error"+e.printStackTrace())
// }
}
}
我尝试在 build.gradle 中添加这些代码但对我没有任何帮助或建议:
compile 'com.squareup.retrofit2:retrofit:2.0.0-SNAPSHOT'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
【问题讨论】:
标签: android retrofit httpexception