简体   繁体   中英

Not able to return the response from an API GET request in a android Kotlin function

I'm currently trying to develop an android app in android studio in Kotlin. I use a RESTful API to GET/POST data to and from my app to a MSSQL database.

In the android studio, I've made a function that sends a GET request to the API using Volley and returns the response from the API in JsonArray format.

The problem I've encountered is that the response is empty(/skipped) which results in returning an empty JsonArray.

The function looks as following:

1 fun apiGetRequest(context: Context, token: String?, id: String?): JsonArray {
2     val queue = Volley.newRequestQueue(context)
3     val url = "http://**.**.*.**:**/****/****?token=$token&id=$id"
4 
5     var jsonArray = JSONArray()
6 
7     val jsonArrayRequest = JsonArrayRequest(url, { response ->
8         jsonArray = response
9     }, {
10        //Error listener
11    })
12    queue.add(jsonArrayRequest)
13    return jsonArray
14}

The URL used in this GET request works and returns the desired JsonArray in Postman. But in this function, the response is either empty or skipped. In debug mode it completely skips row 8 till 11 which results in returning an empty JsonArray.

I've also tried to first break down the response in single JsonObjects and adding it to the JsonArray like this:

7 val jsonArrayRequest = JsonArrayRequest(url, { response ->
8         for(i in 0 until response.length()) {
9             val jsonObject =  response.getJSONObject(i)
10            jsonArray.put(jsonObject)
11        }

But to no result.

I have a feeling that the build-in multithreading in the android studio might be the cause of my problem. But I'm not sure.

If anyone knows a fix or a solution to my problem, I would gladly hear it. Or even a push in the right direction is appreciated.

Thanks in advance.

You cannot return the result synchronously. You have to do it in async way. Try this, I just modified your code little bit.

fun apiGetRequest(context: Context, token: String?, id: String?, callback: (result: JSONArray) -> Unit) {
 val queue = Volley.newRequestQueue(context)
     val url = "http://**.**.*.**:**/****/****?token=$token&id=$id"
 
     //var jsonArray = JSONArray()
 
     val jsonArrayRequest = JsonArrayRequest(url, { response ->
         // jsonArray = response
         callback(response)
     }, {
        //Error listener
        callback(null)
    })
    queue.add(jsonArrayRequest)
    
}



Now you can call this method like this...

apiGetRequest(context ,"your token","your id" ) {
   if(it!=null){
      // println(it)
      // do what you want to do with the jsonarray
   }
   else{
      // show some error message to user or whatever.
   }
    
}


In this way, it is working asynchrously. in apiGetRequest method, when it is calling queue.add(jsonArrayRequest), it does not wait for the call to end. that is why we have to use a callback method, so that when result is ready, it can pass that result to us through that callback.

callback(response) and callback(null) are doing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM