【问题标题】:Kotlin klaxon parse Google direction ApiKotlin klaxon 解析谷歌方向 Api
【发布时间】:2018-04-04 17:15:19
【问题描述】:

对不起我的英语(谷歌翻译)。 我正在尝试在 Kotlin 上使用 klaxon (https://github.com/cbeust/klaxon) 从答案谷歌方向获取两点之间的距离。

 fun distanceDier (start: LatLng, end: LatLng, mode: String) {
    val url = ("http://maps.googleapis.com/maps/api/directions/json?"
            + "origin=" + start.latitude + "," + start.longitude
            + "&destination=" + end.latitude + "," + end.longitude
            + "&sensor=false&units=metric&mode=" + mode)
    val result = URL("$url").readText()
    val parser: Parser = Parser()
    val stringBuilder: StringBuilder = StringBuilder(result)
    val json: JsonObject = parser.parse(stringBuilder) as JsonObject
    println("distance : ${json.string("routes.legs.distance.text")},")
    println("$url") //test

设定点

 distanceDier(LatLng(53.402971, 91.083748),LatLng(53.529799, 91.410684),"TravelMode")

日志:

FATAL EXCEPTION: main
                                                      Process: ru.kbais.coal4, PID: 4494
                                                      java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {ru.kbais.coal4/ru.kbais.coal4.MainActivity}: android.os.NetworkOnMainThreadException

位置错误

val result = URL("$url").readText()

网址:http://maps.googleapis.com/maps/api/directions/json?origin=53.402971,91.083748&destination=53.529799,91.410684&sensor=false&units=metric&mode=TravelMode

如何获取到Json文件的距离,错误码是什么?

【问题讨论】:

  • 我使用了 async { distanceDier (LatLng (53.402971, 91.083748), LatLng (53.529799, 91.410684), "TravelMode") } 但错误仍然存​​在
  • 没有。这不是重复的
  • 如果你不断收到NetworkOnMainThreadException,那仍然是同样的问题。请参阅我为修复链接的问题。
  • 如果异步不起作用,我建议阅读this answer。它有一个很好的选择加上很多解释

标签: json kotlin google-directions-api klaxon


【解决方案1】:
 fun distanceDier (start: LatLng, end: LatLng, mode: String) {
val url = ("http://maps.googleapis.com/maps/api/directions/json?"
        + "origin=" + start.latitude + "," + start.longitude
        + "&destination=" + end.latitude + "," + end.longitude
        + "&sensor=false&units=metric&mode=" + mode)
//val result = URL(url).readText()
//an extension over string (support GET, PUT, POST, DELETE with httpGet(), httpPut(), httpPost(), httpDelete())
url.httpGet().responseString { request, response, result ->
    //do something with response
    when (result) {
        is Result.Failure -> {

        }
        is Result.Success -> {
            val res = result.value
            println("Result: $result")
            val parser: Parser = Parser()
            val stringBuilder: StringBuilder = StringBuilder(res)
            val json: JsonObject = parser.parse(stringBuilder) as JsonObject
            println("distance : ${json.lookup<String?>("routes.legs.distance.text")},")

        }
    }
}
}

【讨论】:

  • 这不起作用。如果你尝试你会得到以下错误,因为 Klaxon 和 Fuel 一起玩不好java.lang.ClassCastException: com.beust.klaxon.JsonArray cannot be cast to java.lang.String
  • 听起来结果是一个数组,所以你应该调用parser.parseArray(stringBuilder)
猜你喜欢
  • 2014-03-26
  • 2011-09-23
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
相关资源
最近更新 更多