Advertisement
tuomasvaltanen

Untitled

Feb 13th, 2023 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. // Edistynyt mobiiliohjelmointi, 13.2.2023
  2.  
  3. // FeedbackReadFragment, testataan tuleeko Directusin data perille
  4. // vaihda JSON_URL:ssa tilalle oman Directus-kantasi URL
  5. // ks. Harjoitus 3 -> Directus
  6.  
  7. class FeedbackReadFragment : Fragment() {
  8. // change this to match your fragment name
  9. private var _binding: FragmentFeedbackReadBinding? = null
  10.  
  11. // This property is only valid between onCreateView and
  12. // onDestroyView.
  13. private val binding get() = _binding!!
  14.  
  15. override fun onCreateView(
  16. inflater: LayoutInflater,
  17. container: ViewGroup?,
  18. savedInstanceState: Bundle?
  19. ): View? {
  20. _binding = FragmentFeedbackReadBinding.inflate(inflater, container, false)
  21. val root: View = binding.root
  22.  
  23. // the binding -object allows you to access views in the layout, textviews etc.
  24.  
  25. getFeedbacks()
  26.  
  27. return root
  28. }
  29.  
  30. fun getFeedbacks(){
  31. // this is the url where we want to get our data
  32. // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
  33. // localhost refers to the Android device instead of your computer
  34. val JSON_URL = "https://xxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
  35.  
  36. // Request a string response from the provided URL.
  37. val stringRequest: StringRequest = object : StringRequest(
  38. Request.Method.GET, JSON_URL,
  39. Response.Listener { response ->
  40.  
  41. Log.d("ADVTECH", response)
  42.  
  43. // response from API, you can use this in TextView, for example
  44. // Check also out the example below
  45.  
  46. // Note: if you send data to API instead, this might not be needed
  47. },
  48. Response.ErrorListener {
  49. // typically this is a connection error
  50. Log.d("ADVTECH", it.toString())
  51. })
  52. {
  53. @Throws(AuthFailureError::class)
  54. override fun getHeaders(): Map<String, String> {
  55. // we have to specify a proper header, otherwise Apigility will block our queries!
  56. // define we are after JSON data!
  57. val headers = HashMap<String, String>()
  58. headers["Accept"] = "application/json"
  59. headers["Content-Type"] = "application/json; charset=utf-8"
  60. return headers
  61. }
  62. }
  63.  
  64. // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
  65. val requestQueue = Volley.newRequestQueue(context)
  66. requestQueue.add(stringRequest)
  67. }
  68.  
  69. override fun onDestroyView() {
  70. super.onDestroyView()
  71. _binding = null
  72. }
  73. }
  74.  
  75. // FeedbackReadFragment, kaivetaan JSON-datasta lista feedbackeja (data-kentän sisällä)
  76.  
  77. var feedbacks : List<Feedback> = emptyList();
  78.  
  79. fun getFeedbacks(){
  80. // this is the url where we want to get our data
  81. // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
  82. // localhost refers to the Android device instead of your computer
  83. val JSON_URL = "https://xxxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
  84. val gson = GsonBuilder().setPrettyPrinting().create()
  85.  
  86. // Request a string response from the provided URL.
  87. val stringRequest: StringRequest = object : StringRequest(
  88. Request.Method.GET, JSON_URL,
  89. Response.Listener { response ->
  90. // Log.d("ADVTECH", response)
  91.  
  92. // haetaan JSONista data-kenttä, joka syötetään sitten GSONille
  93. val jObject = JSONObject(response)
  94. val jArray = jObject.getJSONArray("data")
  95.  
  96. feedbacks = gson.fromJson(jArray.toString() , Array<Feedback>::class.java).toList()
  97.  
  98. for(item in feedbacks) {
  99. Log.d("ADVTECH", item.name.toString())
  100. }
  101.  
  102. // response from API, you can use this in TextView, for example
  103. // Check also out the example below
  104.  
  105. // Note: if you send data to API instead, this might not be needed
  106. },
  107. Response.ErrorListener {
  108. // typically this is a connection error
  109. Log.d("ADVTECH", it.toString())
  110. })
  111. {
  112. @Throws(AuthFailureError::class)
  113. override fun getHeaders(): Map<String, String> {
  114. // we have to specify a proper header, otherwise Apigility will block our queries!
  115. // define we are after JSON data!
  116. val headers = HashMap<String, String>()
  117. headers["Accept"] = "application/json"
  118. headers["Content-Type"] = "application/json; charset=utf-8"
  119. return headers
  120. }
  121. }
  122.  
  123. // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
  124. val requestQueue = Volley.newRequestQueue(context)
  125. requestQueue.add(stringRequest)
  126. }
  127.  
  128.  
  129. // FeedbackReadFragment.kt
  130.  
  131. class FeedbackReadFragment : Fragment() {
  132. // change this to match your fragment name
  133. private var _binding: FragmentFeedbackReadBinding? = null
  134.  
  135. // This property is only valid between onCreateView and
  136. // onDestroyView.
  137. private val binding get() = _binding!!
  138.  
  139. override fun onCreateView(
  140. inflater: LayoutInflater,
  141. container: ViewGroup?,
  142. savedInstanceState: Bundle?
  143. ): View? {
  144. _binding = FragmentFeedbackReadBinding.inflate(inflater, container, false)
  145. val root: View = binding.root
  146.  
  147. // the binding -object allows you to access views in the layout, textviews etc.
  148.  
  149. getFeedbacks()
  150.  
  151. return root
  152. }
  153.  
  154. var feedbacks : List<Feedback> = emptyList();
  155.  
  156. fun getFeedbacks(){
  157. // this is the url where we want to get our data
  158. // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
  159. // localhost refers to the Android device instead of your computer
  160. val JSON_URL = "https://xxxxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
  161. val gson = GsonBuilder().setPrettyPrinting().create()
  162.  
  163. // Request a string response from the provided URL.
  164. val stringRequest: StringRequest = object : StringRequest(
  165. Request.Method.GET, JSON_URL,
  166. Response.Listener { response ->
  167. //Log.d("ADVTECH", response)
  168. val jObject = JSONObject(response)
  169. val jArray = jObject.getJSONArray("data")
  170.  
  171. feedbacks = gson.fromJson(jArray.toString() , Array<Feedback>::class.java).toList()
  172.  
  173. for(item in feedbacks) {
  174. Log.d("ADVTECH", item.name.toString())
  175. }
  176.  
  177. // ListViewistä löytyy perusadaptereita valmiiksi
  178. val adapter = ArrayAdapter(activity as Context, R.layout.simple_list_item_1, feedbacks)
  179. binding.listViewFeedbacks.adapter = adapter
  180. },
  181. Response.ErrorListener {
  182. // typically this is a connection error
  183. Log.d("ADVTECH", it.toString())
  184. })
  185. {
  186. @Throws(AuthFailureError::class)
  187. override fun getHeaders(): Map<String, String> {
  188. // we have to specify a proper header, otherwise Apigility will block our queries!
  189. // define we are after JSON data!
  190. val headers = HashMap<String, String>()
  191. headers["Accept"] = "application/json"
  192. headers["Content-Type"] = "application/json; charset=utf-8"
  193. return headers
  194. }
  195. }
  196.  
  197. // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
  198. val requestQueue = Volley.newRequestQueue(context)
  199. requestQueue.add(stringRequest)
  200. }
  201.  
  202. override fun onDestroyView() {
  203. super.onDestroyView()
  204. _binding = null
  205. }
  206. }
  207.  
  208. // FeedbackReadFragmentin xml-ulkoasu:
  209.  
  210. <?xml version="1.0" encoding="utf-8"?>
  211. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  212. xmlns:tools="http://schemas.android.com/tools"
  213. android:layout_width="match_parent"
  214. android:layout_height="match_parent"
  215. android:orientation="vertical"
  216. tools:context=".FeedbackReadFragment">
  217.  
  218.  
  219. <Button
  220. android:id="@+id/button_send_feedback"
  221. android:layout_width="match_parent"
  222. android:layout_height="wrap_content"
  223. android:text="SEND FEEDBACK" />
  224.  
  225. <ListView
  226. android:id="@+id/listView_feedbacks"
  227. android:layout_width="match_parent"
  228. android:layout_height="match_parent" />
  229.  
  230. </LinearLayout>
  231.  
  232. // jotta palautteet tulostuvat nätisti ListViewiin, yliajetaan Feedback-luokan toString() -metodi:
  233.  
  234. data class Feedback (
  235.  
  236. @SerializedName("id" ) var id : Int? = null,
  237. @SerializedName("name" ) var name : String? = null,
  238. @SerializedName("location" ) var location : String? = null,
  239. @SerializedName("value" ) var value : String? = null
  240.  
  241. )
  242. {
  243. override fun toString(): String {
  244. var result : String = name.toString() + ": " + value.toString()
  245. return result
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement