0

The val queue needs a context and I have to use a context and so I have to use the @Composable so I can write the val context. but the problem is that where I want to use this function, there is not inside a Composable, so I can't use this function.

How can I write a volley request wihtout context or tell me an alternative way to write context here?

    @Composable
  fun MyMainVolley() {

  val context = LocalContext.current
  val queue = Volley.newRequestQueue(context)

  val url = "https://rezaapp.downloadseriesmovie.ir/maintxt.php"
  val stringRequest = StringRequest(
    Request.Method.GET, url,
    { response ->
       baseUrl = response
    },
    { println("That didn't work!") })
 queue.add(stringRequest)
  }
Reza Zeraati
  • 303
  • 1
  • 8

1 Answers1

3

You can pass the Context as parameter.

Something like:

@Composable
fun test1() {

    val context = LocalContext.current
    val response = remember { mutableStateOf("") }

    Column(){
        Button(
           onClick = { MyMainVolley(context,response) }
        ){
        Text("Start volley")
        }
    

       Text(response.value)
    }
}

fun MyMainVolley(
    context: Context,
    result: MutableState<String>
) {

    val queue = Volley.newRequestQueue(context)

    val url = "https://rezaapp.downloadseriesmovie.ir/maintxt.php"
    val stringRequest = StringRequest(
        Request.Method.GET, url,
        { response ->
            result.value = response
        },
        { println("That didn't work!") })
    queue.add(stringRequest)
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • thanks for answering. when I use the MyMainVolley() function, I get the error: Expecting a top level declaration. I want to use this function on top of my retrofit interface, to make my baseurl changable from my php file. is it possible? is my method a good way for doing my goal? – Reza Zeraati Oct 28 '22 at 23:14