tuomasvaltanen

Untitled

Feb 24th, 2023 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. // Edistynyt mobiiliohjelmointi 24.2.2023
  2.  
  3. Tarvittavat fragmentit tässä harjoituksessa (tee navigation editorin kautta)
  4.  
  5. - FeedbackReadFragment (lisää päävalikkoon)
  6. - ota käyttöön binding layer (esim. DataReadFragmentista mallia)
  7. - Lisätään ulkoasuun pelkästään ListView, id:ksi listView_feedbacks
  8.  
  9. - FeedbackSendFragment
  10. - ota käyttöön binding layer (esim. DataReadFragmentista mallia)
  11. - lisätään päävalikkoon, lisätään EditTextejä jokaiselle feedbackin osalle (name, location, value) ) -> Plain Text
  12.  
  13. - BasicAuthFragment
  14. - Lisätään päävalikkoon, binding layer käyttöön, ei parametreja
  15.  
  16. // Directusin ollessa huollossa, tehdään Basic Auth lisätehtävä:
  17.  
  18. https://apingweb.com/#basic
  19.  
  20. Testataan Insomnia Portablella kaikki operaatiot läpi ennen kuin mennään Androidiin koodaamaan.
  21. Tämä siksi että tiedetään varmasti miten rajapinta toimii ettei turhaan etsitä ongelmaa Androidin päässä.
  22.  
  23. Hae kaikki userit:
  24. https://apingweb.com/api/auth/users (GET-metodi), Basic Auth -> admin/12345
  25.  
  26. Hae vain yksi user:
  27. https://apingweb.com/api/auth/users/1 (GET-metodi), Basic Auth -> admin/12345
  28.  
  29. Lähetä uusi user:
  30. https://apingweb.com/api/auth/users (POST-metodi), Basic Auth -> admin/12345
  31.  
  32. JSON Body ->
  33. {
  34. "name": "Tester Person",
  35. "age": "32",
  36. "image": "https:\/\/example.com\/something.png",
  37. "email": "someone@somewhere.com"
  38. }
  39.  
  40. Päivitetään olemassa oleva user (hae jokin id oikeasta datasta kun haet kaikki userit)
  41.  
  42. Vaihda 197 mihin id:hen haluat, eli se määrää mitä id:tä muokataan
  43. https://apingweb.com/api/auth/user/edit/197 (PUT-metodi), Basic Auth -> admin/12345
  44.  
  45. JSON Body, eli uudet tiedot mitkä päivitetään tilalle:
  46. {
  47. "name": "Updated Person",
  48. "age": "39",
  49. "image": "https:\/\/example.com\/somethingupdate.png",
  50. "email": "someoneupdated@somewhere.com"
  51. }
  52.  
  53. Userin poistaminen:
  54. https://apingweb.com/api/auth/user/delete/197 (DELETE-metodi), Basic Auth -> admin/12345
  55. HUOM: id:n pitää olla olemassa, eli ei voi enää poistaa jo poistettua useria.
  56.  
  57.  
  58. // Tehdään uusi Fragment -> BasicAuthFragment (navigaatioeditorilla) -> lisätään päävalikkoon
  59.  
  60.  
  61. // BasicAuthFragment.kt:
  62.  
  63. // VERSIO 1 - EI BASIC AUTHIA (yleensä rajapinnat eivät toimi ilman mitään kirjautumista)
  64.  
  65. class BasicAuthFragment : Fragment() {
  66. // change this to match your fragment name
  67. private var _binding: FragmentBasicAuthBinding? = null
  68.  
  69. // This property is only valid between onCreateView and
  70. // onDestroyView.
  71. private val binding get() = _binding!!
  72.  
  73. override fun onCreateView(
  74. inflater: LayoutInflater,
  75. container: ViewGroup?,
  76. savedInstanceState: Bundle?
  77. ): View? {
  78. _binding = FragmentBasicAuthBinding.inflate(inflater, container, false)
  79. val root: View = binding.root
  80.  
  81. // the binding -object allows you to access views in the layout, textviews etc.
  82.  
  83. getUsers()
  84.  
  85. return root
  86. }
  87.  
  88. fun getUsers() {
  89.  
  90. val JSON_URL = "https://apingweb.com/api/users"
  91.  
  92. // Request a string response from the provided URL.
  93. val stringRequest: StringRequest = object : StringRequest(
  94. Request.Method.GET, JSON_URL,
  95. Response.Listener { response ->
  96. Log.d("TESTI", response)
  97.  
  98. // jos käyttöliittymässä on textview nimeltä textView_raw_user_data:
  99. //binding.textViewRawuserdata.text = response
  100. },
  101. Response.ErrorListener {
  102. // typically this is a connection error
  103. Log.d("TESTI", it.toString())
  104. })
  105. {
  106. @Throws(AuthFailureError::class)
  107. override fun getHeaders(): Map<String, String> {
  108. // we have to specify a proper header, otherwise Apigility will block our queries!
  109. // define we are after JSON data!
  110. val headers = HashMap<String, String>()
  111. headers["Accept"] = "application/json"
  112. headers["Content-Type"] = "application/json; charset=utf-8"
  113. return headers
  114. }
  115. }
  116.  
  117. // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
  118. val requestQueue = Volley.newRequestQueue(context)
  119. requestQueue.add(stringRequest)
  120. }
  121.  
  122. override fun onDestroyView() {
  123. super.onDestroyView()
  124. _binding = null
  125. }
  126. }
  127.  
  128. // VERSIO 2 -> Basic auth, käyttäjätunnus admin ja salasana 12345
  129.  
  130. class BasicAuthFragment : Fragment() {
  131. // change this to match your fragment name
  132. private var _binding: FragmentBasicAuthBinding? = null
  133.  
  134. // This property is only valid between onCreateView and
  135. // onDestroyView.
  136. private val binding get() = _binding!!
  137.  
  138. override fun onCreateView(
  139. inflater: LayoutInflater,
  140. container: ViewGroup?,
  141. savedInstanceState: Bundle?
  142. ): View? {
  143. _binding = FragmentBasicAuthBinding.inflate(inflater, container, false)
  144. val root: View = binding.root
  145.  
  146. // the binding -object allows you to access views in the layout, textviews etc.
  147.  
  148. getUsers()
  149.  
  150. return root
  151. }
  152.  
  153. val username = "admin"
  154. val password = "12345"
  155.  
  156. fun getUsers() {
  157.  
  158. val JSON_URL = "https://apingweb.com/api/auth/users"
  159.  
  160. // Request a string response from the provided URL.
  161. val stringRequest: StringRequest = object : StringRequest(
  162. Request.Method.GET, JSON_URL,
  163. Response.Listener { response ->
  164. Log.d("TESTI", response)
  165.  
  166. // jos käyttöliittymässä on textview nimeltä textView_raw_user_data:
  167. binding.textViewRawuserdata.text = response
  168. },
  169. Response.ErrorListener {
  170. // typically this is a connection error
  171. Log.d("TESTI", it.toString())
  172. })
  173. {
  174. @Throws(AuthFailureError::class)
  175. override fun getHeaders(): Map<String, String> {
  176. // we have to specify a proper header, otherwise Apigility will block our queries!
  177. // define we are after JSON data!
  178. val headers = HashMap<String, String>()
  179. headers["Accept"] = "application/json"
  180. headers["Content-Type"] = "application/json; charset=utf-8"
  181.  
  182. val authorizationString = "Basic " + Base64.encodeToString(
  183. (username + ":" + password).toByteArray(), Base64.DEFAULT
  184. )
  185.  
  186. headers.put("Authorization", authorizationString)
  187.  
  188. // Insomniassa voit kokeilla ottaa pois Basic Auth, ja laittaa headerseihin
  189. // Authorization ja arvoksi tämä tulostettu string
  190. Log.d("TESTI", authorizationString)
  191.  
  192. return headers
  193. }
  194. }
  195.  
  196. // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
  197. val requestQueue = Volley.newRequestQueue(context)
  198. requestQueue.add(stringRequest)
  199. }
  200.  
  201. override fun onDestroyView() {
  202. super.onDestroyView()
  203. _binding = null
  204. }
  205. }
  206.  
  207.  
  208. // FeedbackReedFragmentin ulkoasu, lisätään ListView
  209.  
  210. <?xml version="1.0" encoding="utf-8"?>
  211. <FrameLayout 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. tools:context=".FeedbackReadFragment">
  216.  
  217.  
  218. <ListView
  219. android:id="@+id/listView_feedbacks"
  220. android:layout_width="match_parent"
  221. android:layout_height="match_parent" />
  222. </FrameLayout>
  223.  
  224. // FeedbackSendFragmentin ulkoasu, lisätään EditTextit
  225.  
  226. <?xml version="1.0" encoding="utf-8"?>
  227. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  228. xmlns:tools="http://schemas.android.com/tools"
  229. android:layout_width="match_parent"
  230. android:layout_height="match_parent"
  231. android:orientation="vertical"
  232. android:layout_margin="10dp"
  233. tools:context=".FeedbackSendFragment">
  234.  
  235.  
  236. <TextView
  237. android:layout_width="match_parent"
  238. android:layout_height="wrap_content"
  239. android:text="Name"
  240. android:textStyle="bold" />
  241.  
  242. <EditText
  243. android:id="@+id/editText_feedback_name"
  244. android:layout_width="match_parent"
  245. android:layout_height="wrap_content"
  246. android:ems="10"
  247. android:hint="Your name"
  248. android:inputType="textPersonName" />
  249.  
  250. <TextView
  251. android:layout_width="match_parent"
  252. android:layout_height="wrap_content"
  253. android:text="Location"
  254. android:textStyle="bold" />
  255.  
  256. <EditText
  257. android:id="@+id/editText_feedback_location"
  258. android:layout_width="match_parent"
  259. android:layout_height="wrap_content"
  260. android:ems="10"
  261. android:hint="Your location"
  262. android:inputType="textPersonName" />
  263.  
  264. <TextView
  265. android:layout_width="match_parent"
  266. android:layout_height="wrap_content"
  267. android:text="Feedback"
  268. android:textStyle="bold" />
  269.  
  270. <EditText
  271. android:id="@+id/editText_feedback_value"
  272. android:layout_width="match_parent"
  273. android:layout_height="wrap_content"
  274. android:ems="10"
  275. android:hint="Your feedback text..."
  276. android:inputType="textPersonName" />
  277.  
  278. <Button
  279. android:id="@+id/button_send_feedback"
  280. android:layout_width="match_parent"
  281. android:layout_height="wrap_content"
  282. android:text="SEND FEEDBACK" />
  283. </LinearLayout>
  284.  
  285. // FeedbackReadFragment, jatketaan tästä ensi kerralla
  286.  
  287. class FeedbackReadFragment : Fragment() {
  288. // TODO: Rename and change types of parameters
  289. // change this to match your fragment name
  290. private var _binding: FragmentFeedbackReadBinding? = null
  291.  
  292. // This property is only valid between onCreateView and
  293. // onDestroyView.
  294. private val binding get() = _binding!!
  295.  
  296. override fun onCreateView(
  297. inflater: LayoutInflater,
  298. container: ViewGroup?,
  299. savedInstanceState: Bundle?
  300. ): View? {
  301. _binding = FragmentFeedbackReadBinding.inflate(inflater, container, false)
  302. val root: View = binding.root
  303.  
  304.  
  305. // the binding -object allows you to access views in the layout, textviews etc.
  306.  
  307. getFeedbacks()
  308.  
  309. return root
  310. }
  311.  
  312. fun getFeedbacks() {
  313. // tähän tulee lopuksi Volley-koodi, jolla haetaan dataa Directusista
  314. // Directusin data on JSONia, ja muunnamme sen käyttökelpoiseen muotoon
  315. // GSONilla. Tätä varten tarvitaan dataluokka json2kt.comin kautta.
  316. }
  317.  
  318. override fun onDestroyView() {
  319. super.onDestroyView()
  320. _binding = null
  321. }
  322. }
  323.  
  324. // FeedbackSendFragment, jatketaan tästä ensi kerralla
  325.  
  326. class FeedbackSendFragment : Fragment() {
  327. // TODO: Rename and change types of parameters
  328. // change this to match your fragment name
  329. private var _binding: FragmentFeedbackSendBinding? = null
  330.  
  331. // This property is only valid between onCreateView and
  332. // onDestroyView.
  333. private val binding get() = _binding!!
  334.  
  335. override fun onCreateView(
  336. inflater: LayoutInflater,
  337. container: ViewGroup?,
  338. savedInstanceState: Bundle?
  339. ): View? {
  340. _binding = FragmentFeedbackSendBinding.inflate(inflater, container, false)
  341. val root: View = binding.root
  342.  
  343. binding.buttonSendFeedback.setOnClickListener {
  344. var testing = "EditText, arvot: \n"
  345.  
  346. // testataan että saadaan seuraavalla luennolla haettua EditTextien
  347. // datat Volleytä varten
  348. testing += binding.editTextFeedbackName.text.toString()
  349. testing += "\n"
  350. testing += binding.editTextFeedbackLocation.text.toString()
  351. testing += "\n"
  352. testing += binding.editTextFeedbackValue.text.toString()
  353.  
  354. Log.d("TESTI", testing)
  355. }
  356.  
  357. // the binding -object allows you to access views in the layout, textviews etc.
  358.  
  359. return root
  360. }
  361.  
  362. fun sendFeedback() {
  363. // tähän koodi, joka käynnistetään napin kautta (Submit)
  364. // lähetetään POST-kysely Volleylla Directusiin
  365. // bodyna uusi data JSON-muodossa ilman id:tä (käytetään GSONia
  366. // muuntamaan data JSONIksi)
  367.  
  368. // haetaan uuden feedbackin tiedot EditTexteistä (3 kpl)
  369. }
  370.  
  371. override fun onDestroyView() {
  372. super.onDestroyView()
  373. _binding = null
  374. }
  375. }
Add Comment
Please, Sign In to add comment