0

i'm beginner with kotlin. I want to get my json file from an url but isn't working.

class: MyRequest

var json:JsonObject = JsonObject()

init {
    val queue = Volley.newRequestQueue(context)
    val request = JsonObjectRequest(
        Request.Method.GET, url, null,
        { response -> json
        },
        { error ->
            println(error)
        }
    )
    queue.add(request)
    queue.start()
}

class: Database

val url: String = "http://...."

fun test(){
    var r: MyRequest = MyRequest(context, url)
    println(r.json)
}

In this exemple, i just want to print my json but i've "{}"

Thank you for helping me

Smooki
  • 35
  • 1
  • 6
  • `response -> json` does this even do anything ? `response -> foo = response ` is how you would assign the response – a_local_nobody Oct 26 '21 at 10:48
  • for your information "{}" is actually valid a valid json response. It's an empty json object. Are you sure your url actually returns something else? – Ivo Oct 26 '21 at 13:56
  • @IvoBeckers the full url -> http://os-vps418.infomaniak.ch:1186/i507_2_2/movie_db.json – Smooki Oct 26 '21 at 14:41
  • @a_local_nobody i try response -> json = response But no change – Smooki Oct 26 '21 at 14:45
  • i'm not sure if your code is synchronous, try logging your `response` variable to see if that works. if it has the data you're looking for, this link will help you get it -> https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value – a_local_nobody Oct 26 '21 at 14:47
  • @a_local_nobody that a good question, i think my program don't read response -> json – Smooki Oct 26 '21 at 14:52
  • Try using `RequestFuture` : `RequestFuture future = RequestFuture.newFuture();` And then future.get(); – Sox - Oct 29 '21 at 02:41

1 Answers1

0

Can't you get the json from the url or can't you print the json you get? I don't quite understand it.

If your question about print json, json is an object and you can't print it directly, you have to convert it to String

fun test(){
    var r: MyRequest = MyRequest(context, url)
    r = jsonObject.toString()
                System.out.println(r)
}

try this way please.

if problem is getting json from url please tell me.

GoldBerg
  • 106
  • 5
  • I want to get a jsonobject, the print it's just for test – Smooki Oct 26 '21 at 12:12
  • you should look here [link](https://stackoverflow.com/questions/64817901/http-json-request-in-android-r-and-kotlin) * same codes, same problem, same solution – GoldBerg Oct 26 '21 at 13:31
  • i don't have any error, i just don't know how to get the json – Smooki Oct 26 '21 at 14:38
  • it's not the same problem, nor is it the same solution. in the post you linked, they're trying to set the value of the response onto something on the UI, while here, OP wants to return it as the result of a function. this code probably isn't synchronous, which means that you can't return it as part of a function – a_local_nobody Oct 26 '21 at 14:51
  • hem, i don't understand all – Smooki Oct 26 '21 at 14:54