Volley JsonObjectRequest - Android Kotlin

Volley JsonObjectRequest - Android Kotlin - Hello friend inabnomaniiyaha, In the article that you read this time with the title Volley JsonObjectRequest - Android Kotlin, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel kotlin,what we write you can understand. Alright, happy reading.

Judul : Volley JsonObjectRequest - Android Kotlin
link : Volley JsonObjectRequest - Android Kotlin

Baca juga


Volley JsonObjectRequest - Android Kotlin

Tags: Volley JsonObjectRequest, Volley JsonObjectRequest Kotlin, Volley JsonObjectRequest Android example, Kotlin Volley JsonObjectRequest, How to Volley JsonObjectRequest kotlin example.

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        textView.movementMethod = ScrollingMovementMethod()

        val url = "https://pastebin.com/raw/2bW31yqa"

        button.setOnClickListener {

            progressBar.visibility = View.VISIBLE

            val request = JsonObjectRequest(

                Request.Method.GET, // method

                url, // url

                null, // json request

                { response -> // response listener

                    try {

                        val obj: JSONObject = response

                        val array = obj.getJSONArray("students")

                        textView.text = ""

                        for (i in 0 until  array.length()){

                            val student: JSONObject = array.getJSONObject(i)

                            val firstName: String = student.getString("firstname")

                            val lastName: String = student.getString("lastname")

                            val age: Int = student.getInt("age")

                            textView.append("$firstName $lastName\nage : $age\n\n")

                        }

                    }catch (e: JSONException){

                        textView.text = e.message

                    }

                    progressBar.visibility = View.INVISIBLE

                },

                { error -> 

                    textView.text = error.message

                    progressBar.visibility = View.INVISIBLE

                }

            )

            VolleySingleton.getInstance(applicationContext)

                .addToRequestQueue(request)

        }

    }

}

VolleySingleton.kt

class VolleySingleton constructor(context: Context) {

    companion object {

        @Volatile

        private var INSTANCE: VolleySingleton? = null

        fun getInstance(context: Context) =

            INSTANCE ?: synchronized(this) {

                INSTANCE ?: VolleySingleton(context).also {

                    INSTANCE = it

                }

            }

    }

    val imageLoader: ImageLoader by lazy {

        ImageLoader(requestQueue,

            object : ImageLoader.ImageCache {

                private val cache = LruCache<String, Bitmap>(20)

                override fun getBitmap(url: String): Bitmap {

                    return cache.get(url)

                }

                override fun putBitmap(url: String, bitmap: Bitmap) {

                    cache.put(url, bitmap)

                }

            })

    }

    private val requestQueue: RequestQueue by lazy {

        Volley.newRequestQueue(context.applicationContext)

    }

    fun <T> addToRequestQueue(req: Request<T>) {

        requestQueue.add(req)

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/constraintLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#EDEAE0"

    tools:context=".MainActivity">


    <Button

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"

        android:text="Get Json Object"

        android:textAllCaps="false"

        android:backgroundTint="#3B2F2F"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />


    <ProgressBar

        android:id="@+id/progressBar"

        style="?android:attr/progressBarStyle"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"

        android:visibility="invisible"

        app:layout_constraintBottom_toBottomOf="@+id/button"

        app:layout_constraintStart_toEndOf="@+id/button"

        app:layout_constraintTop_toTopOf="@+id/button" />


    <TextView

        android:id="@+id/textView"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginStart="8dp"

        android:layout_marginTop="12dp"

        android:layout_marginEnd="8dp"

        android:fontFamily="sans-serif-condensed-medium"

        android:padding="8dp"

        android:textColor="#4F42B5"

        android:textSize="22sp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/button"

        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>

app.gradle [dependencies]

implementation 'com.android.volley:volley:1.1.1'
Volley JsonObjectRequest



That's the articleVolley JsonObjectRequest - Android Kotlin

That's it for the article Volley JsonObjectRequest - Android Kotlin this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleVolley JsonObjectRequest - Android Kotlin with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/11/volley-jsonobjectrequest-android-kotlin.html

0 Response to "Volley JsonObjectRequest - Android Kotlin"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...