通过Retrofit和GSON发布原始json

时间:2018-12-15 17:59:42

标签: android json kotlin retrofit

我想问,是否有通过改装将GSON使用原始json发送到服务器?

这是我要发送的Json

{
    "polygon": {
        "type": "Polygon",
        "coordinates":  [ 
            [
                [8.594874265234353, 49.33654935186479],
                [8.594874265234353, 49.322858939564284],
                [8.553675534765603, 49.322858939564284],
                [8.553675534765603, 49.33654935186479],
                [8.594874265234353, 49.33654935186479]
            ]

        ]
    },
    "placeType": 10,
    "title": "Mr"

}

这是我的改造电话。

@POST("place/filter") fun get(@Body jsonObject: JsonObject): Call<PlaceResult>

这是我的代码。

        val projection = map.projection.visibleRegion.latLngBounds

        val northEast = LatLng(projection.northeast.latitude, projection.northeast.longitude) // North East of the Screen.
        val northWest = LatLng(projection.southwest.latitude, projection.northeast.longitude) // North West of the Screen.
        val southWest = LatLng(projection.southwest.latitude, projection.southwest.longitude) // South West of the Screen.
        val southEast = LatLng(projection.northeast.latitude, projection.southwest.longitude) // South East of the Screen.

        val jsonArray = JsonArray()

        jsonArray.add(JsonArray())
        jsonArray.add(JsonArray())
        jsonArray.add(JsonArray())
        jsonArray.add(JsonArray())
        jsonArray.add(JsonArray())

        jsonArray[0].asJsonArray.add(northEast.latitude)
        jsonArray[0].asJsonArray.add(northEast.longitude)
        jsonArray[1].asJsonArray.add(northWest.latitude)
        jsonArray[1].asJsonArray.add(northWest.longitude)
        jsonArray[2].asJsonArray.add(southWest.latitude)
        jsonArray[2].asJsonArray.add(southWest.longitude)
        jsonArray[3].asJsonArray.add(southEast.latitude)
        jsonArray[3].asJsonArray.add(southEast.longitude)
        jsonArray[4].asJsonArray.add(northEast.latitude)
        jsonArray[4].asJsonArray.add(northEast.longitude)

        val jsonObject = JsonObject()
        jsonObject.addProperty("type", "Polygon")
        jsonObject.addProperty("placeType", 10)
        jsonObject.addProperty("title", "Mr")
        jsonObject.add("coordinates", jsonArray)

        ApiClient.getApiService().get(jsonObject).enqueue(object : retrofit2.Callback<PlaceResult> {
            override fun onFailure(call: Call<PlaceResult>, t: Throwable) {
                util.logInfo("TAG", t.message.toString())
            }

            override fun onResponse(call: Call<PlaceResult>, response: Response<PlaceResult>) {
                util.logInfo("TAG", response.code().toString())
            }
        })

问题是,运行此代码后,好像什么都没有发生,onResponseonFailure都没有被调用。

再说一次,如果有人有更好的解决方案,我会适当的。

谢谢。

0 个答案:

没有答案