-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoAPIController.kt
152 lines (134 loc) · 6.05 KB
/
DemoAPIController.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* A controller to interact with the API for fetching drink data
*/
class APIController (var context: Context) {
/**
* Fetches a list of drinks by drink type (beer or wine) from the API.
* @param drink: The type of drink, "beer" or "wine"
* */
fun getAllByDrinkType(drink: String){
// The API URL for retrieving drink details by type.
var url = ""
if (drink.equals("beer", ignoreCase = true)) {
url = ""
} else if (drink.equals("wine", ignoreCase = true)) {
url = ""
}
// Create a new request using Volley
val queue: RequestQueue = Volley.newRequestQueue(context)
// A JSON string request to fetch the data
val request = StringRequest(
Request.Method.POST,
url,
{ response ->
try {
// Parse the response into a List of Drink objects using Gson
val jArray = JSONArray(response)
val DrinkType = object : TypeToken<List<Drink>>() {}.type
val drinks: List<Drink> = Gson().fromJson(jArray.toString(), DrinkType)
// Start SubMenuActivity and pass the fetched data
val intent = Intent(context, SubMenuScreen::class.java)
intent.putParcelableArrayListExtra("drinkList", ArrayList(drinks))
context.startActivity(intent)
// LogCat message for response
Log.e("API Response", response.toString())
// LogCat message for parsed data
Log.d("Parsed Data", drinks.toString())
} catch (e: JSONException) {
e.printStackTrace()
// Handle JSON parsing error
Toast.makeText(context, "Error parsing JSON", Toast.LENGTH_LONG).show()
}
},
{ error ->
Toast.makeText(context, "Error: ${error.message}", Toast.LENGTH_LONG).show()
})
queue.add(request)
}
/**
* Retrieves drink details by drink ID from the API
*
* @param drink - The ID of the drink
* @param onDrinkNameReceived - Callback function invoked for drink name and image URL.
* */
fun getAllDrinksById(drink: Int, onDrinkNameReceived: (String, String?) -> Unit) {
//The API provides an Array with Objects.
val drinkIdURL = ""
val queue: RequestQueue = Volley.newRequestQueue(context)
// Adding a key-value pair to the JSON Object.
val jsonObject = JSONObject()
jsonObject.put("drinkId", drink)
// A JSON array request to fetch the data
val request = JsonArrayRequest(
Request.Method.POST,
drinkIdURL,
null,
{ response ->
try {
// Parse the JSON response into a list of Drink objects using Gson
val drinks: List<Drink> = Gson().fromJson(
response.toString(),
object : TypeToken<List<Drink>>() {}.type
)
// Find the specific drink by matching the provided drink ID
val targetDrink = drinks.find { it.id == drink }
// If the target drink is found, extract name and image URL
if(targetDrink != null){
val imageUrl = targetDrink.image
targetDrink.name?.let { onDrinkNameReceived(it, imageUrl) }
}
// LogCat messages for API response and parsed data
Log.e("API Response", response.toString())
Log.d("Parsed Data", drinks.toString())
} catch (e: JSONException) {
e.printStackTrace()
// Show a toast message if there's an error parsing JSON
Toast.makeText(context, "Error parsing JSON", Toast.LENGTH_LONG).show()
}
},
// Error message
Response.ErrorListener { error ->
Toast.makeText(context, "Error: ${error.message}", Toast.LENGTH_LONG).show()
}
)
// Add the request to the RequestQueue to initiate the API call
queue.add(request)
}
fun getDataScan(id: String, callback: (Drink?) -> Unit) {
val url = ""
// Create a new request queue
val queue: RequestQueue = Volley.newRequestQueue(context)
// Used to get JSON data
val gson = Gson()
// Make an API request to fetch the data
val request = StringRequest(
Request.Method.POST,
url,
{ response ->
try {
Log.d("Server Response", response)
// Parse the JSON response, allows you to work with the data
val jsonObject = JSONObject(response)
// Get the data object from the JSON response
val dataObject = jsonObject.getJSONObject("data")
// Get the drink object from the data object
val drinkArray = dataObject.getJSONArray("drink")
// Parse the first drink object from the array
val drink = gson.fromJson(drinkArray.get(0).toString(), Drink::class.java)
Log.d("Parsed Drink", drink?.toString() ?: "null")
callback(drink) // Invoke the callback with the drink data
} catch (e: Exception) {
Log.e("Exception", e.message, e)
e.printStackTrace()
Toast.makeText(context, "Fout bij het verwerken van de gegevens", Toast.LENGTH_LONG).show()
callback(null) // Invoke the callback with null if an error occurred
}
},
{ error ->
error.printStackTrace()
Toast.makeText(context, "Geen respons", Toast.LENGTH_LONG).show()
callback(null) // Invoke the callback with null if there was no response
})
queue.add(request)
}
}