Retrofit 2 请求没有给我数据
Retrofit 2 request doesn't give me data
我正在尝试通过 Retrofit 2 发送 GET 请求。
但是,该请求没有执行任何操作..
API 服务
package com.example.brews.network
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlinx.coroutines.Deferred
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
/*
This is the sandbox base url (way less data than production environment)
When deploying app -> use production base url
*/
private const val BASE_URL = "https://sandbox-api.brewerydb.com/v2/"
/**
* Build the Moshi object that Retrofit will be using, making sure to add the Kotlin adapter for
* full Kotlin compatibility.
*/
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
/**
* Use the Retrofit builder to build a retrofit object using a Moshi converter with our Moshi
* object.
*/
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
/**
* A public interface that exposes the [getProperties] method
*/
interface BreweryApiService {
/**
* Returns a Coroutine [Deferred] [List] of [BeerProperty] which can be fetched with await() if
* in a Coroutine scope.
* The @GET annotation indicates that the "beers" endpoint will be requested with the GET
* HTTP method
*/
@GET("beers/")
fun getProperties(@Query("?key") key: String):
// The Coroutine Call Adapter allows us to return a Deferred, a Job with a result
Deferred<List<BeerProperty>>
}
/**
* A public Api object that exposes the lazy-initialized Retrofit service
*/
object BreweryApi {
val retrofitService: BreweryApiService by lazy { retrofit.create(BreweryApiService::class.java) }
}
数据CLASS
package com.example.brews.network
data class BeerProperty(
val id: Int,
val name: String
)
填满我列表的方法
private fun getBeersProperties() {
coroutineScope.launch {
var getPropertiesDeferred =
BreweryApi.retrofitService.getProperties("13e9caaf80adac04dce90ef55600d898")
try {
_status.value = BreweryApiStatus.LOADING
val listResult = getPropertiesDeferred.await()
_status.value = BreweryApiStatus.DONE
_properties.value = listResult
} catch (e: Exception) {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
}
}
由link
检索到的JSON
{
"currentPage": 1,
"numberOfPages": 23,
"totalResults": 1109,
"data":[
{
"id": "c4f2KE",
"name": "'穆里坎比尔森啤酒",
"nameDisplay": "'Murican 比尔森啤酒",
"abv": "5.5",
"glasswareId": 4,
"styleId": 98,
"isOrganic": "N",
"isRetired": "N"
}
]
}
我需要检索的是'Data'里面的'ID'和'Name'。但是,这是在一个数组中,我不知道如何通过改造来提取它..
您需要有一个 DAO 对象来从改造中获取 JSON 响应,然后解析 JSON 对象以获得您想要的结果。
所以像这样创建一个 DAO 对象:-
data class BeerResponse(
val data: List<BeerProperty>?
)
并将您的服务方式更改为:-
@GET("beers/")
fun getProperties(@Query("?key") key: String):
// The Coroutine Call Adapter allows us to return a Deferred, a Job with a result
Deferred<BeerResponse>
然后在您的 getBeerProperties 方法中,将其更改为:-
private fun getBeersProperties() {
coroutineScope.launch {
var getPropertiesDeferred =
BreweryApi.retrofitService.getProperties("13e9caaf80adac04dce90ef55600d898")
try {
_status.value = BreweryApiStatus.LOADING
val listResult = getPropertiesDeferred.await()
listResult.data?.let {
_status.value = BreweryApiStatus.DONE
_properties.value = it
} ?: let {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
} catch (e: Exception) {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
}
您对网络的实现 API 是错误的,应该改成这样
data class BeerProperty(
val id: Int,
val name: String
)
data class Response(
val data: List<BeerProperty>?
)
interface BreweryApiService {
@GET("beers/")
fun getProperties(@Query("?key") key: String):
Deferred<Response>
}
您还可以在响应中输入页数、当前页和... class
此外,您可以在 android studio 中使用 JSON to kotlin class 插件来使这些数据 class 更快并且错误更少,您还可以使用 [=11 这样的网站=] 以更易读的漂亮格式查看 JSON
我正在尝试通过 Retrofit 2 发送 GET 请求。
但是,该请求没有执行任何操作..
API 服务
package com.example.brews.network
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlinx.coroutines.Deferred
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
/*
This is the sandbox base url (way less data than production environment)
When deploying app -> use production base url
*/
private const val BASE_URL = "https://sandbox-api.brewerydb.com/v2/"
/**
* Build the Moshi object that Retrofit will be using, making sure to add the Kotlin adapter for
* full Kotlin compatibility.
*/
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
/**
* Use the Retrofit builder to build a retrofit object using a Moshi converter with our Moshi
* object.
*/
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
/**
* A public interface that exposes the [getProperties] method
*/
interface BreweryApiService {
/**
* Returns a Coroutine [Deferred] [List] of [BeerProperty] which can be fetched with await() if
* in a Coroutine scope.
* The @GET annotation indicates that the "beers" endpoint will be requested with the GET
* HTTP method
*/
@GET("beers/")
fun getProperties(@Query("?key") key: String):
// The Coroutine Call Adapter allows us to return a Deferred, a Job with a result
Deferred<List<BeerProperty>>
}
/**
* A public Api object that exposes the lazy-initialized Retrofit service
*/
object BreweryApi {
val retrofitService: BreweryApiService by lazy { retrofit.create(BreweryApiService::class.java) }
}
数据CLASS
package com.example.brews.network
data class BeerProperty(
val id: Int,
val name: String
)
填满我列表的方法
private fun getBeersProperties() {
coroutineScope.launch {
var getPropertiesDeferred =
BreweryApi.retrofitService.getProperties("13e9caaf80adac04dce90ef55600d898")
try {
_status.value = BreweryApiStatus.LOADING
val listResult = getPropertiesDeferred.await()
_status.value = BreweryApiStatus.DONE
_properties.value = listResult
} catch (e: Exception) {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
}
}
由link
检索到的JSON{
"currentPage": 1,
"numberOfPages": 23,
"totalResults": 1109,
"data":[
{
"id": "c4f2KE",
"name": "'穆里坎比尔森啤酒",
"nameDisplay": "'Murican 比尔森啤酒",
"abv": "5.5",
"glasswareId": 4,
"styleId": 98,
"isOrganic": "N",
"isRetired": "N"
}
]
}
我需要检索的是'Data'里面的'ID'和'Name'。但是,这是在一个数组中,我不知道如何通过改造来提取它..
您需要有一个 DAO 对象来从改造中获取 JSON 响应,然后解析 JSON 对象以获得您想要的结果。
所以像这样创建一个 DAO 对象:-
data class BeerResponse(
val data: List<BeerProperty>?
)
并将您的服务方式更改为:-
@GET("beers/")
fun getProperties(@Query("?key") key: String):
// The Coroutine Call Adapter allows us to return a Deferred, a Job with a result
Deferred<BeerResponse>
然后在您的 getBeerProperties 方法中,将其更改为:-
private fun getBeersProperties() {
coroutineScope.launch {
var getPropertiesDeferred =
BreweryApi.retrofitService.getProperties("13e9caaf80adac04dce90ef55600d898")
try {
_status.value = BreweryApiStatus.LOADING
val listResult = getPropertiesDeferred.await()
listResult.data?.let {
_status.value = BreweryApiStatus.DONE
_properties.value = it
} ?: let {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
} catch (e: Exception) {
_status.value = BreweryApiStatus.ERROR
_properties.value = ArrayList()
}
}
您对网络的实现 API 是错误的,应该改成这样
data class BeerProperty(
val id: Int,
val name: String
)
data class Response(
val data: List<BeerProperty>?
)
interface BreweryApiService {
@GET("beers/")
fun getProperties(@Query("?key") key: String):
Deferred<Response>
}
您还可以在响应中输入页数、当前页和... class 此外,您可以在 android studio 中使用 JSON to kotlin class 插件来使这些数据 class 更快并且错误更少,您还可以使用 [=11 这样的网站=] 以更易读的漂亮格式查看 JSON