Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Edistynyt mobiiliohjelmointi 24.2.2023
- Tarvittavat fragmentit tässä harjoituksessa (tee navigation editorin kautta)
- - FeedbackReadFragment (lisää päävalikkoon)
- - ota käyttöön binding layer (esim. DataReadFragmentista mallia)
- - Lisätään ulkoasuun pelkästään ListView, id:ksi listView_feedbacks
- - FeedbackSendFragment
- - ota käyttöön binding layer (esim. DataReadFragmentista mallia)
- - lisätään päävalikkoon, lisätään EditTextejä jokaiselle feedbackin osalle (name, location, value) ) -> Plain Text
- - BasicAuthFragment
- - Lisätään päävalikkoon, binding layer käyttöön, ei parametreja
- // Directusin ollessa huollossa, tehdään Basic Auth lisätehtävä:
- https://apingweb.com/#basic
- Testataan Insomnia Portablella kaikki operaatiot läpi ennen kuin mennään Androidiin koodaamaan.
- Tämä siksi että tiedetään varmasti miten rajapinta toimii ettei turhaan etsitä ongelmaa Androidin päässä.
- Hae kaikki userit:
- https://apingweb.com/api/auth/users (GET-metodi), Basic Auth -> admin/12345
- Hae vain yksi user:
- https://apingweb.com/api/auth/users/1 (GET-metodi), Basic Auth -> admin/12345
- Lähetä uusi user:
- https://apingweb.com/api/auth/users (POST-metodi), Basic Auth -> admin/12345
- JSON Body ->
- {
- "name": "Tester Person",
- "age": "32",
- "image": "https:\/\/example.com\/something.png",
- "email": "someone@somewhere.com"
- }
- Päivitetään olemassa oleva user (hae jokin id oikeasta datasta kun haet kaikki userit)
- Vaihda 197 mihin id:hen haluat, eli se määrää mitä id:tä muokataan
- https://apingweb.com/api/auth/user/edit/197 (PUT-metodi), Basic Auth -> admin/12345
- JSON Body, eli uudet tiedot mitkä päivitetään tilalle:
- {
- "name": "Updated Person",
- "age": "39",
- "image": "https:\/\/example.com\/somethingupdate.png",
- "email": "someoneupdated@somewhere.com"
- }
- Userin poistaminen:
- https://apingweb.com/api/auth/user/delete/197 (DELETE-metodi), Basic Auth -> admin/12345
- HUOM: id:n pitää olla olemassa, eli ei voi enää poistaa jo poistettua useria.
- // Tehdään uusi Fragment -> BasicAuthFragment (navigaatioeditorilla) -> lisätään päävalikkoon
- // BasicAuthFragment.kt:
- // VERSIO 1 - EI BASIC AUTHIA (yleensä rajapinnat eivät toimi ilman mitään kirjautumista)
- class BasicAuthFragment : Fragment() {
- // change this to match your fragment name
- private var _binding: FragmentBasicAuthBinding? = null
- // This property is only valid between onCreateView and
- // onDestroyView.
- private val binding get() = _binding!!
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = FragmentBasicAuthBinding.inflate(inflater, container, false)
- val root: View = binding.root
- // the binding -object allows you to access views in the layout, textviews etc.
- getUsers()
- return root
- }
- fun getUsers() {
- val JSON_URL = "https://apingweb.com/api/users"
- // Request a string response from the provided URL.
- val stringRequest: StringRequest = object : StringRequest(
- Request.Method.GET, JSON_URL,
- Response.Listener { response ->
- Log.d("TESTI", response)
- // jos käyttöliittymässä on textview nimeltä textView_raw_user_data:
- //binding.textViewRawuserdata.text = response
- },
- Response.ErrorListener {
- // typically this is a connection error
- Log.d("TESTI", it.toString())
- })
- {
- @Throws(AuthFailureError::class)
- override fun getHeaders(): Map<String, String> {
- // we have to specify a proper header, otherwise Apigility will block our queries!
- // define we are after JSON data!
- val headers = HashMap<String, String>()
- headers["Accept"] = "application/json"
- headers["Content-Type"] = "application/json; charset=utf-8"
- return headers
- }
- }
- // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
- val requestQueue = Volley.newRequestQueue(context)
- requestQueue.add(stringRequest)
- }
- override fun onDestroyView() {
- super.onDestroyView()
- _binding = null
- }
- }
- // VERSIO 2 -> Basic auth, käyttäjätunnus admin ja salasana 12345
- class BasicAuthFragment : Fragment() {
- // change this to match your fragment name
- private var _binding: FragmentBasicAuthBinding? = null
- // This property is only valid between onCreateView and
- // onDestroyView.
- private val binding get() = _binding!!
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = FragmentBasicAuthBinding.inflate(inflater, container, false)
- val root: View = binding.root
- // the binding -object allows you to access views in the layout, textviews etc.
- getUsers()
- return root
- }
- val username = "admin"
- val password = "12345"
- fun getUsers() {
- val JSON_URL = "https://apingweb.com/api/auth/users"
- // Request a string response from the provided URL.
- val stringRequest: StringRequest = object : StringRequest(
- Request.Method.GET, JSON_URL,
- Response.Listener { response ->
- Log.d("TESTI", response)
- // jos käyttöliittymässä on textview nimeltä textView_raw_user_data:
- binding.textViewRawuserdata.text = response
- },
- Response.ErrorListener {
- // typically this is a connection error
- Log.d("TESTI", it.toString())
- })
- {
- @Throws(AuthFailureError::class)
- override fun getHeaders(): Map<String, String> {
- // we have to specify a proper header, otherwise Apigility will block our queries!
- // define we are after JSON data!
- val headers = HashMap<String, String>()
- headers["Accept"] = "application/json"
- headers["Content-Type"] = "application/json; charset=utf-8"
- val authorizationString = "Basic " + Base64.encodeToString(
- (username + ":" + password).toByteArray(), Base64.DEFAULT
- )
- headers.put("Authorization", authorizationString)
- // Insomniassa voit kokeilla ottaa pois Basic Auth, ja laittaa headerseihin
- // Authorization ja arvoksi tämä tulostettu string
- Log.d("TESTI", authorizationString)
- return headers
- }
- }
- // Add the request to the RequestQueue. This has to be done in both getting and sending new data.
- val requestQueue = Volley.newRequestQueue(context)
- requestQueue.add(stringRequest)
- }
- override fun onDestroyView() {
- super.onDestroyView()
- _binding = null
- }
- }
- // FeedbackReedFragmentin ulkoasu, lisätään ListView
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".FeedbackReadFragment">
- <ListView
- android:id="@+id/listView_feedbacks"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </FrameLayout>
- // FeedbackSendFragmentin ulkoasu, lisätään EditTextit
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_margin="10dp"
- tools:context=".FeedbackSendFragment">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Name"
- android:textStyle="bold" />
- <EditText
- android:id="@+id/editText_feedback_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Your name"
- android:inputType="textPersonName" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Location"
- android:textStyle="bold" />
- <EditText
- android:id="@+id/editText_feedback_location"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Your location"
- android:inputType="textPersonName" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Feedback"
- android:textStyle="bold" />
- <EditText
- android:id="@+id/editText_feedback_value"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="Your feedback text..."
- android:inputType="textPersonName" />
- <Button
- android:id="@+id/button_send_feedback"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="SEND FEEDBACK" />
- </LinearLayout>
- // FeedbackReadFragment, jatketaan tästä ensi kerralla
- class FeedbackReadFragment : Fragment() {
- // TODO: Rename and change types of parameters
- // change this to match your fragment name
- private var _binding: FragmentFeedbackReadBinding? = null
- // This property is only valid between onCreateView and
- // onDestroyView.
- private val binding get() = _binding!!
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = FragmentFeedbackReadBinding.inflate(inflater, container, false)
- val root: View = binding.root
- // the binding -object allows you to access views in the layout, textviews etc.
- getFeedbacks()
- return root
- }
- fun getFeedbacks() {
- // tähän tulee lopuksi Volley-koodi, jolla haetaan dataa Directusista
- // Directusin data on JSONia, ja muunnamme sen käyttökelpoiseen muotoon
- // GSONilla. Tätä varten tarvitaan dataluokka json2kt.comin kautta.
- }
- override fun onDestroyView() {
- super.onDestroyView()
- _binding = null
- }
- }
- // FeedbackSendFragment, jatketaan tästä ensi kerralla
- class FeedbackSendFragment : Fragment() {
- // TODO: Rename and change types of parameters
- // change this to match your fragment name
- private var _binding: FragmentFeedbackSendBinding? = null
- // This property is only valid between onCreateView and
- // onDestroyView.
- private val binding get() = _binding!!
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- _binding = FragmentFeedbackSendBinding.inflate(inflater, container, false)
- val root: View = binding.root
- binding.buttonSendFeedback.setOnClickListener {
- var testing = "EditText, arvot: \n"
- // testataan että saadaan seuraavalla luennolla haettua EditTextien
- // datat Volleytä varten
- testing += binding.editTextFeedbackName.text.toString()
- testing += "\n"
- testing += binding.editTextFeedbackLocation.text.toString()
- testing += "\n"
- testing += binding.editTextFeedbackValue.text.toString()
- Log.d("TESTI", testing)
- }
- // the binding -object allows you to access views in the layout, textviews etc.
- return root
- }
- fun sendFeedback() {
- // tähän koodi, joka käynnistetään napin kautta (Submit)
- // lähetetään POST-kysely Volleylla Directusiin
- // bodyna uusi data JSON-muodossa ilman id:tä (käytetään GSONia
- // muuntamaan data JSONIksi)
- // haetaan uuden feedbackin tiedot EditTexteistä (3 kpl)
- }
- override fun onDestroyView() {
- super.onDestroyView()
- _binding = null
- }
- }
Add Comment
Please, Sign In to add comment