I have a PHP-File that get me the last ID from a MySQL Database. The File result is 42.
If i write this in Android (Kotlin):
var lastID: String? = null
val queue = Volley.newRequestQueue(this)
val url = "https://uwbeinternational.org/android/getLastSPLId.php"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
lastID = response
Toast.makeText(applicationContext, "ID Lautet: " lastID, Toast.LENGTH_LONG).show()
},
{ lastID = "Fail!" })
queue.add(stringRequest)
It works and th Toast show "42" to me. But, how i can use the "lastID" as String outside of the Function? If i try to write:
var lastID: String? = null
val queue = Volley.newRequestQueue(this)
val url = "https://uwbeinternational.org/android/getLastSPLId.php"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
lastID = response
},
{ lastID = "Fail!" })
queue.add(stringRequest)
Toast.makeText(applicationContext, "ID Lautet: " lastID, Toast.LENGTH_LONG).show()
The Result (lastID) show "null". What can or should i do to get the right response "lastID" with "42" to a outside String/Variable?
CodePudding user response:
in my opinion, you need change your PHP result, from HTML to REST API with JSON Data.
In Android you must make HTTP Client, you can use Retrofit or Fast Android Networking. if you like Volley just go on
CodePudding user response:
You are displaying your toast before Volley got the change to communicate with php and get a response.
Keep your Toast where it was.
Or only toast after you got the response.
For a test keep both toasts and you will see.