-1

Sorry for my English(google translate). I'm trying to get a distance between two points from the answer google directions using klaxon (https://github.com/cbeust/klaxon) on Kotlin.

 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

setpoints

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

Log:

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

place error

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

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

How to get the distance from the Json file and what is the error code?

  • Possible duplicate of [How do I fix android.os.NetworkOnMainThreadException?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) – msrd0 Oct 24 '17 at 03:10
  • I used async { distanceDier (LatLng (53.402971, 91.083748), LatLng (53.529799, 91.410684), "TravelMode") } but the error has remained – MrBazilevs Oct 24 '17 at 03:20
  • no. this is not a duplicate – MrBazilevs Oct 24 '17 at 03:21
  • If you continuously get `NetworkOnMainThreadException` than that's still the same problem. See the question I've linked for fixes. – msrd0 Oct 24 '17 at 03:22
  • If async doesn't work, I'd recommend reading [this answer](https://stackoverflow.com/a/21284021/3755692). It has a good alternative plus lots of explanations – msrd0 Oct 24 '17 at 03:25

1 Answers1

0
 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")},")

        }
    }
}
}
  • This doesn't work. If you try you get the following error, because Klaxon and Fuel don't play nice together `java.lang.ClassCastException: com.beust.klaxon.JsonArray cannot be cast to java.lang.String` – Peter Weyand Feb 19 '18 at 23:44
  • Sounds like the result is an array, so you should call `parser.parseArray(stringBuilder)`. – Cedric Beust Mar 08 '18 at 19:16