Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Edistynyt mobiiliohjelmointi, 13.2.2023
- // FeedbackReadFragment, testataan tuleeko Directusin data perille
- // vaihda JSON_URL:ssa tilalle oman Directus-kantasi URL
- // ks. Harjoitus 3 -> Directus
- class FeedbackReadFragment : Fragment() {
- // 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(){
- // this is the url where we want to get our data
- // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
- // localhost refers to the Android device instead of your computer
- val JSON_URL = "https://xxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
- // Request a string response from the provided URL.
- val stringRequest: StringRequest = object : StringRequest(
- Request.Method.GET, JSON_URL,
- Response.Listener { response ->
- Log.d("ADVTECH", response)
- // response from API, you can use this in TextView, for example
- // Check also out the example below
- // Note: if you send data to API instead, this might not be needed
- },
- Response.ErrorListener {
- // typically this is a connection error
- Log.d("ADVTECH", 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
- }
- }
- // FeedbackReadFragment, kaivetaan JSON-datasta lista feedbackeja (data-kentän sisällä)
- var feedbacks : List<Feedback> = emptyList();
- fun getFeedbacks(){
- // this is the url where we want to get our data
- // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
- // localhost refers to the Android device instead of your computer
- val JSON_URL = "https://xxxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
- val gson = GsonBuilder().setPrettyPrinting().create()
- // Request a string response from the provided URL.
- val stringRequest: StringRequest = object : StringRequest(
- Request.Method.GET, JSON_URL,
- Response.Listener { response ->
- // Log.d("ADVTECH", response)
- // haetaan JSONista data-kenttä, joka syötetään sitten GSONille
- val jObject = JSONObject(response)
- val jArray = jObject.getJSONArray("data")
- feedbacks = gson.fromJson(jArray.toString() , Array<Feedback>::class.java).toList()
- for(item in feedbacks) {
- Log.d("ADVTECH", item.name.toString())
- }
- // response from API, you can use this in TextView, for example
- // Check also out the example below
- // Note: if you send data to API instead, this might not be needed
- },
- Response.ErrorListener {
- // typically this is a connection error
- Log.d("ADVTECH", 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)
- }
- // FeedbackReadFragment.kt
- class FeedbackReadFragment : Fragment() {
- // 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
- }
- var feedbacks : List<Feedback> = emptyList();
- fun getFeedbacks(){
- // this is the url where we want to get our data
- // Note: if using a local server, use http://10.0.2.2 for localhost. this is a virtual address for Android emulators, since
- // localhost refers to the Android device instead of your computer
- val JSON_URL = "https://xxxxxxxxx.directus.app/items/feedback?access_token=${BuildConfig.DIRECTUS_ACCESS_TOKEN}"
- val gson = GsonBuilder().setPrettyPrinting().create()
- // Request a string response from the provided URL.
- val stringRequest: StringRequest = object : StringRequest(
- Request.Method.GET, JSON_URL,
- Response.Listener { response ->
- //Log.d("ADVTECH", response)
- val jObject = JSONObject(response)
- val jArray = jObject.getJSONArray("data")
- feedbacks = gson.fromJson(jArray.toString() , Array<Feedback>::class.java).toList()
- for(item in feedbacks) {
- Log.d("ADVTECH", item.name.toString())
- }
- // ListViewistä löytyy perusadaptereita valmiiksi
- val adapter = ArrayAdapter(activity as Context, R.layout.simple_list_item_1, feedbacks)
- binding.listViewFeedbacks.adapter = adapter
- },
- Response.ErrorListener {
- // typically this is a connection error
- Log.d("ADVTECH", 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
- }
- }
- // FeedbackReadFragmentin xml-ulkoasu:
- <?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"
- tools:context=".FeedbackReadFragment">
- <Button
- android:id="@+id/button_send_feedback"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="SEND FEEDBACK" />
- <ListView
- android:id="@+id/listView_feedbacks"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
- // jotta palautteet tulostuvat nätisti ListViewiin, yliajetaan Feedback-luokan toString() -metodi:
- data class Feedback (
- @SerializedName("id" ) var id : Int? = null,
- @SerializedName("name" ) var name : String? = null,
- @SerializedName("location" ) var location : String? = null,
- @SerializedName("value" ) var value : String? = null
- )
- {
- override fun toString(): String {
- var result : String = name.toString() + ": " + value.toString()
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement