是否有一种方法可以直接在接口中解析改装响应并使用函数解析响应



我是kotlin的新手,我正在开发kotlin应用程序,我正在使用改装来解析api中的数据。现在我把数据作为json对象返回,因为它不是一个列表,我决定使用标量转换器将数据作为字符串返回。这是我的ApiService文件:

private val retrofit = Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(BASE_URL)
.build()
interface NasaApiService {
@GET("neo/rest/v1/feed?api_key=$API_KEY")
fun getProperties():
Call<String>
}
object NasaApi {
val retrofitService : NasaApiService by lazy {
retrofit.create(NasaApiService::class.java)
}
}

现在要将其转换为我想要的json,我有一个函数可以将此字符串数据更改为我想要的json数据。当获得结果时,我在viewModel中使用它。:

class MainViewModel : ViewModel() {
private val _asteroids = MutableLiveData<List<Asteroid>>()
val asteroid: LiveData<List<Asteroid>> get() = _asteroids
private val _navigateToAsteroidDetails = MutableLiveData<Long>()
val navigateToAsteroidDetails
get() = _navigateToAsteroidDetails
init {
NasaApi.retrofitService.getProperties().enqueue( object: Callback<String> {
override fun onResponse(call: Call<String>, response: Response<String>) {
_asteroids.value = response.body()?.let { parseAsteroidsJsonResult(it) }
}
override fun onFailure(call: Call<String>, t: Throwable) {
TODO("Not yet implemented")
}
})
}
fun onAsteroidItemClicked(id: Long) {
_navigateToAsteroidDetails.value = id
}
fun onAsteroidItemDetailsNavigated() {
_navigateToAsteroidDetails.value = null
}
}

现在我的问题是,在获得响应后,我是否可以直接在NasaApiService接口中解析数据,并返回我创建的数据类型小行星的列表,而不是解析viewModel中的数据。我一直在找,但找不到它在不在那里。如果你想知道json的响应是什么,在这里:

{
"links": {
"next": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-08-16&end_date=2021-08-23&detailed=false&api_key=dj906WfMa1KJzZ0d9fY5KdYE45ZUQniVdptWBd3r",
"prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-08-02&end_date=2021-08-09&detailed=false&api_key=dj906WfMa1KJzZ0d9fY5KdYE45ZUQniVdptWBd3r",
"self": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-08-09&end_date=2021-08-16&detailed=false&api_key=dj906WfMa1KJzZ0d9fY5KdYE45ZUQniVdptWBd3r"
},
"element_count": 80,
"near_earth_objects": {
"2021-08-11": [
{
"links": {
"self": "http://www.neowsapp.com/rest/v1/neo/3092269?api_key=dj906WfMa1KJzZ0d9fY5KdYE45ZUQniVdptWBd3r"
},
"id": "3092269",
"neo_reference_id": "3092269",
"name": "(2000 PN8)",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3092269",
"absolute_magnitude_h": 22.1,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 0.1010543415,
"estimated_diameter_max": 0.2259643771
},
"meters": {
"estimated_diameter_min": 101.054341542,
"estimated_diameter_max": 225.9643771094
},
"miles": {
"estimated_diameter_min": 0.0627922373,
"estimated_diameter_max": 0.140407711
},
"feet": {
"estimated_diameter_min": 331.5431259047,
"estimated_diameter_max": 741.3529669956
}
},
"is_potentially_hazardous_asteroid": false,
"close_approach_data": [
{
"close_approach_date": "2021-08-11",
"close_approach_date_full": "2021-Aug-11 15:22",
"epoch_date_close_approach": 1628695320000,
"relative_velocity": {
"kilometers_per_second": "13.3546688941",
"kilometers_per_hour": "48076.8080188273",
"miles_per_hour": "29873.0588492541"
},
"miss_distance": {
"astronomical": "0.0914372304",
"lunar": "35.5690826256",
"kilometers": "13678814.906539248",
"miles": "8499621.4502397024"
},
"orbiting_body": "Earth"
}
],
"is_sentry_object": false
},
{
"links": {
"self": "http://www.neowsapp.com/rest/v1/neo/3297182?api_key=dj906WfMa1KJzZ0d9fY5KdYE45ZUQniVdptWBd3r"
},
"id": "3297182",
"neo_reference_id": "3297182",
"name": "(2005 UE1)",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3297182",
"absolute_magnitude_h": 26.2,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 0.0152951935,
"estimated_diameter_max": 0.0342010925
},
"meters": {
"estimated_diameter_min": 15.2951935344,
"estimated_diameter_max": 34.201092472
},
"miles": {
"estimated_diameter_min": 0.0095039897,
"estimated_diameter_max": 0.021251567
},
"feet": {
"estimated_diameter_min": 50.1810827555,
"estimated_diameter_max": 112.2083122258
}
},
"is_potentially_hazardous_asteroid": false,
...

从这些数据中,我实际上只需要近地天体列表中的所有数据。只要我给它需要的字符串数据,这个函数就能做到。下面是我用来解析的函数,以便您可以尝试理解更多。

fun parseAsteroidsJsonResult(response: String): ArrayList<Asteroid> {
val jsonResult = JSONObject(response.toString())
val nearEarthObjectsJson = jsonResult.getJSONObject("near_earth_objects")
val asteroidList = ArrayList<Asteroid>()
val nextSevenDaysFormattedDates = getNextSevenDaysFormattedDates()
for (formattedDate in nextSevenDaysFormattedDates) {
val dateAsteroidJsonArray = nearEarthObjectsJson.getJSONArray(formattedDate)
for (i in 0 until dateAsteroidJsonArray.length()) {
val asteroidJson = dateAsteroidJsonArray.getJSONObject(i)
val id = asteroidJson.getLong("id")
val codename = asteroidJson.getString("name")
val absoluteMagnitude = asteroidJson.getDouble("absolute_magnitude_h")
val estimatedDiameter = asteroidJson.getJSONObject("estimated_diameter")
.getJSONObject("kilometers").getDouble("estimated_diameter_max")
val closeApproachData = asteroidJson
.getJSONArray("close_approach_data").getJSONObject(0)
val relativeVelocity = closeApproachData.getJSONObject("relative_velocity")
.getDouble("kilometers_per_second")
val distanceFromEarth = closeApproachData.getJSONObject("miss_distance")
.getDouble("astronomical")
val isPotentiallyHazardous = asteroidJson
.getBoolean("is_potentially_hazardous_asteroid")
val asteroid = Asteroid(id, codename, formattedDate, absoluteMagnitude,
estimatedDiameter, relativeVelocity, distanceFromEarth, isPotentiallyHazardous)
asteroidList.add(asteroid)
}
}
return asteroidList
}

在模块级gradle文件中导入以下依赖项:

implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

按如下方式声明响应数据类:

data class ExampleResponse (
@SerializedName("links")
val linkes: String? = null
)

添加GsonConverterFactory到Retrofit实例:

Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(GsonConverterFactory.create())
.build()

最后声明接口:

interface ExampleApiService {
@GET("news")
fun getNews(): ExampleResponse
}

最新更新