Java 改装2请求不';不要给我数据
我正试图通过改造2发送GET请求 但是,请求没有任何作用 API服务Java 改装2请求不';不要给我数据,java,android,kotlin,retrofit,retrofit2,Java,Android,Kotlin,Retrofit,Retrofit2,我正试图通过改造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.KotlinJsonAdapterFac
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) }
}
填写我的列表的方法
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()
}
}
}
链接检索到的JSON
{
“当前页面”:1,
“页数”:23页,
“总体结果”:1109,
“数据”:[
{
“id”:“c4f2KE”,
“名称”:“穆里坎·皮尔斯纳”,
“名称显示”:“穆里坎·皮尔斯纳”,
“abv”:“5.5”,
“glasswareId”:4,
“styleId”:98,
“同构”:“N”,
“isRetired”:“N”
}] }
我需要检索的是“数据”中的“ID”和“Name”。但是,这是在一个数组中,我不知道如何使用Reformation提取它。您需要一个DAO对象来从Reformation获取JSON响应,然后解析JSON对象以获得您想要的结果 因此,创建一个DAO对象,如下所示:-
data class BeerResponse(
val data: List<BeerProperty>?
)
您需要有一个DAO对象来从改造中获得JSON响应,然后解析JSON对象以获得所需的结果 因此,创建一个DAO对象,如下所示:-
data class BeerResponse(
val data: List<BeerProperty>?
)
您对网络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>
}
数据类BeerProperty(
val id:Int,
val名称:String
)
数据类响应(
val数据:列表?
)
YapiService接口{
@获得(“啤酒/”)
fun getProperties(@Query(“?key”)键:字符串):
推迟
}
你也可以把页数,当前页面和。。。在反应课上
此外,您可以在android studio中使用JSON到kotlin类插件来加快这些数据类的速度并减少错误,您还可以使用一些网站,如查看更具可读性的prety格式的JSON
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>
}
数据类BeerProperty(
val id:Int,
val名称:String
)
数据类响应(
val数据:列表?
)
YapiService接口{
@获得(“啤酒/”)
fun getProperties(@Query(“?key”)键:字符串):
推迟
}
你也可以把页数,当前页面和。。。在反应课上
此外,您可以在android studio中使用JSON到kotlin类插件,以使这些数据类更快、错误更少,您还可以使用类似于以更具可读性的prety格式查看JSON的网站
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>
}