Last active
June 3, 2025 10:28
-
-
Save walteranyika/bf671e609abf27a5ae01af5f7e884749 to your computer and use it in GitHub Desktop.
Volley Codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Add this permission in Manifest file | |
| //<uses-permission android:name="android.permission.INTERNET"/> | |
| //https://mature-octopus-causal.ngrok-free.app/api/people/new | |
| //Code to send data to server | |
| fun sendData( | |
| context: Context, | |
| names: String, | |
| //add all other fields here | |
| onSuccess: () -> Unit, | |
| onError: (String) -> Unit | |
| ) { | |
| val url = "ADD UR DJANGO/BE/NGROK ENDPOINT HERE" | |
| val requestQueue = Volley.newRequestQueue(context) | |
| val jsonBody = JSONObject().apply { | |
| put("names", names) | |
| //Add all fields here | |
| } | |
| val jsonRequest = object : JsonObjectRequest( | |
| Method.POST, url, jsonBody, | |
| Response.Listener { response -> | |
| Log.d("VolleyResponse", "Server response: $response") | |
| onSuccess() | |
| }, | |
| Response.ErrorListener { error -> | |
| Log.e("VolleyError", "Error response: ${error.message}", error) | |
| onError(error.message ?: "Unknown error") | |
| } | |
| ) { | |
| override fun getHeaders(): MutableMap<String, String> { | |
| val headers = HashMap<String, String>() | |
| headers["Content-Type"] = "application/json" | |
| return headers | |
| } | |
| } | |
| requestQueue.add(jsonRequest) | |
| } | |
| //Code to fetch from the server | |
| fun fetchUsers( | |
| context: Context, | |
| onSuccess: (List<User>) -> Unit, | |
| onError: (String) -> Unit | |
| ) { | |
| val url = "ADD UR DJANGO/BE/NGROK ENDPOINT HERE" // Replace with your real URL | |
| val requestQueue = Volley.newRequestQueue(context) | |
| val jsonArrayRequest = JsonArrayRequest( | |
| Request.Method.GET, url, null, | |
| { response -> | |
| val users = mutableListOf<User>() | |
| for (i in 0 until response.length()) { | |
| val obj = response.getJSONObject(i) | |
| val user = User( | |
| id = obj.getInt("id"), | |
| names = obj.getString("names"), | |
| //Parse all other fields here | |
| ) | |
| users.add(user) | |
| } | |
| onSuccess(users) | |
| }, | |
| { error -> | |
| onError(error.message ?: "Error fetching users") | |
| } | |
| ) | |
| requestQueue.add(jsonArrayRequest) | |
| } | |
| //Code for dropdown menu | |
| ExposedDropdownMenuBox(expanded = expanded, onExpandedChange = { expanded = !expanded }) { | |
| OutlinedTextField( | |
| value = selectedEduLevel, | |
| onValueChange = {}, | |
| readOnly = true, | |
| label = { Text("Education Level") }, | |
| trailingIcon = { | |
| ExposedDropdownMenuDefaults.TrailingIcon(expanded = true) | |
| }, | |
| modifier = Modifier | |
| .fillMaxWidth() | |
| .menuAnchor(type=MenuAnchorType.PrimaryEditable, enabled = true) | |
| ) | |
| ExposedDropdownMenu( | |
| expanded = expanded, | |
| onDismissRequest = { expanded = false }) { | |
| eduLevels.forEach { level -> | |
| DropdownMenuItem( | |
| text = { Text(level) }, | |
| onClick = { | |
| selectedEduLevel = level | |
| expanded = false | |
| } | |
| ) | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment