Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

android - Retrofit & Mochi: Empty response (and weird bug)

I'm having the weirdest bug I EVER had.

For a school project, I need to get the JSON of this API. I didn't encounter any error, but the response body is null. Not exactly null, but still null. First, here is the code I use to get the data (Yeah, it's in the UI thread but this is not my main problem)

    val moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()

        val retrofit: Retrofit = Retrofit.Builder()
            .baseUrl("http://barcelonaapi.marcpous.com")
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build()

        val service = retrofit.create(StationAPI::class.java)
        val call = service.stationsList()

        call.enqueue(object : Callback<StationResponse> {
            override fun onResponse(
                call: Call<StationResponse>,
                response: Response<StationResponse>
            ) {
                val statusCode: Int = response.code()
                val resp: StationResponse? = response.body()

                Log.d("JSON", "Status code: $statusCode")
                Log.d("JSON", "Resp: $resp")
            }

            override fun onFailure(call: Call<StationResponse>?, t: Throwable) {
                t.printStackTrace()
            }
        })

And here is my data classes:

interface StationAPI {
    @GET("/bus/nearstation/latlon/41.3985182/2.1917991/1.json")
    fun stationsList(): Call<StationResponse>
}

data class StationResponse (
    @field:Json(name = "code") var code: Int,
    @field:Json(name = "data") var data: NearStation
)

data class NearStation(
    @field:Json(name = "nearstations") var stations: List<Station>
)

data class Station(
    @field:Json(name = "id") var id: Long,
    @field:Json(name = "street_name") var streetName: String?,
    @field:Json(name = "city") var city: String?,
    @field:Json(name = "utm_x") var utmX: String?,
    @field:Json(name = "utm_y") var utmY: String?,
    @field:Json(name = "lat") var latitude: String?,
    @field:Json(name = "lon") var longitude: String?,
    @field:Json(name = "furniture") var furniture: String?,
    @field:Json(name = "buses") var buses: String?,
    @field:Json(name = "distance") var distance: Float,
)

In onResponse(), the response code is always equal to 0 (default value for Int) and data is always null. BUT, now the fun begins. If I comment @field:Json(name = "data") var data: NearStation, the code parameter is now 200 as it should be.

And to add more fun to the fun cake, a friend has exactly the same code but it's working for him.

Please help, I don't understand.

question from:https://stackoverflow.com/questions/65648395/retrofit-mochi-empty-response-and-weird-bug

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use @Json annotation instead of java field annotation @field:Json. It should be like this

data class StationResponse (
    @Json(name = "code") var code: Int,
    @Json(name = "data") var data: NearStation
)

data class NearStation(
    @Json(name = "nearstations") var stations: List<Station>
)

data class Station(
    @Json(name = "id") var id: Long,
    @Json(name = "street_name") var streetName: String?,
    @Json(name = "city") var city: String?,
    @Json(name = "utm_x") var utmX: String?,
    @Json(name = "utm_y") var utmY: String?,
    @Json(name = "lat") var latitude: String?,
    @Json(name = "lon") var longitude: String?,
    @Json(name = "furniture") var furniture: String?,
    @Json(name = "buses") var buses: String?,
    @Json(name = "distance") var distance: Float,
)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...