3

I am new to Kotlin. I am simply trying to return a response as a String from a method. But if I use val Str = ""; it is not re-assignable.

Like Java in why can't we re-assign a response to a String which is defined already, and return it.

public fun getCustomers(): String {
  val Str = null;
  val StringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response ->
    Str = response.toString();

  }, Response.ErrorListener {
    it.printStackTrace();
  })

  return Str;
}

2 Answers 2

6

Use var for the Str variable that you have used here because val is like final, and you can't re-assign it.

public fun getCustomers(): String {
  var Str = "";
  val StringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response ->
    Str = response.toString();

  }, Response.ErrorListener {
    it.printStackTrace();
  })

  return Str;
}
Sign up to request clarification or add additional context in comments.

Comments

0
private val customers: String get() {
    var Str = "";
    val StringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response ->
        Str = response.toString();
    }, Response.ErrorListener {
        it.printStackTrace();
    })
    return Str;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.