0

I request your help in solving this pesky problem. I am trying to return the value of 'response' returned by the REST API call, to the calling program. I get null string. Here is the code for your reference. In this code, I want the value of the variable 'response' returned to the caller in onCreate, ie 'restApiResponse' variable. Thank you very much. -Vittal PS: I am a newbie to Kotlin/Android programming.

class OpeningActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_opening)

        var restApiResponse = getAPIResponse() // <<<< restApiResponse is empty string


        val submitButton = findViewById<Button>(R.id.submitBtn)
        submitButton.setOnClickListener() {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }

    }

fun getAPIResponse() : String {

        val myUrl = "YOUR REST API CALL URL"
        var jsonObject = JSONObject();
        val queue = Volley.newRequestQueue(this);

        cpyResponse = StringBuilder();

        val stringRequest = object: StringRequest(Request.Method.GET, myUrl,
                Response.Listener<String> { response ->
                    Log.d("A", "VK:  Response is: " + response.substring(0, 500))
                    cpyResponse.append(response.toString())
                    // .. cpyResponse now has the content of response.
                },
                Response.ErrorListener { })
        {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers.put("X-API-KEY", "r0IS395C2D8ITjSKV05F610yPXsDQZjllmprr");
                return headers
            }
        }
        queue.add(stringRequest)

        Log.d("A", "VK: Response is: " + response) // <<<<-- value of response is gone.. it is an empty string!!!!!! :( 
        return cpyResponse.toString()  // <<< --- cpyResource is also empty string
    }
user2876642
  • 205
  • 2
  • 10
  • 1
    it is asynchronous( will take some time) you get the response. But you log immediately so you see empty string. Why is this question tagged with kotlincoroutines? – Raghunandan Nov 24 '20 at 07:22
  • I am getting a valid reply and the variable 'response' has the correct content. This log line shows the correct output: "Log.d("A", "VK: Response is: " + response.substring(0, 500)) cpyResponse.append(response.toString())" But the last log line in getAPIResponse() shows 'response' to be empty. This is where I request your help . And, sorry I will remove the coroutines tag (if I can) – user2876642 Nov 24 '20 at 17:52
  • 1
    you can do what you want in the callback itself. `Response.Listener { response -> here do something with response` – Raghunandan Nov 25 '20 at 03:25
  • I tried your suggestion. I want to send this response string to another view where I render the contents ( name, address, contact number etc). But, it didnt work. Looks like 'response' gets cleaned up ( perhaps it is on the function stack which unwinds as it exits). Copying this value to another variable also doesnt seem to work. Question is "How do I retain the content of 'response' for later use?" – user2876642 Nov 25 '20 at 18:56

0 Answers0