Im very new with Kotlin/Android development. Im creating an app login activity that will pass three values that must be submitted to a web server for authentication. I initially created a fun "jsonParse()" within the MainActivity.kt and it worked just fine with authentication and json response. But I would like to create this fun as a public class "ApiRequest()" that I can call from the MainActivity by passing the 3 parameters as follows: ApiRequest.jsonparse(value1,value2,value3)
This is my current ApiRequest() public class
import android.app.Application
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import org.json.JSONException
class ApiRequest : Application() {
fun jsonParse(coid: String, user: String, pwd: String) {
val requestQueue : RequestQueue = Volley.newRequestQueue(applicationContext)
val url = AppConstant.endpoint_url + "auth/androidLogin.asp?coid=xcoid&username=xuser&password=xpwd"
println("JSON URL: $url")
val request = JsonObjectRequest(Request.Method.GET, url, null, {
response ->try {
val jsonArray = response.getJSONArray("Login")
for (i in 0 until jsonArray.length()) {
val login = jsonArray.getJSONObject(i)
//... process code here of json response...
}
} catch (e: JSONException) {
e.printStackTrace()
}
}, { error -> error.printStackTrace()
})
requestQueue.add(request)
}
companion object {
fun jsonParse(coid: String, user: String, pwd: String) {
val xcoid = coid
var xuser = user
var xpwd = pwd
}
}
}
My issue is that the jsonObjectRequest is not getting processed as Im not sure how to pass those parameters to be part of the url at the top of the class. I did check from the "companion object" at the bottom of the class that the parameters are getting passed but not read by the function. Again, very new at this and any help with coding this properly would be greatly appreciated.