Introduction to Volley libraries

Volley Android के लिए Google की HTTP library है: यह network calls के hard parts handle करती है, इन्हें main thread से हटकर run करते हुए, एक queue manage करते हुए, और results callbacks को deliver करते हुए, तो आप कुछ lines में एक API request बनाते हैं।

10 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Networking को Manageable बनाया गया

Android से हाथ से एक API call करना surprisingly fiddly है: network calls को main thread से हटकर run करना पड़ता है (नहीं तो app freeze हो जाती है), और इनके results को UI update करने के लिए main thread पर वापस आना पड़ता है। वह threading खुद manage करना error-prone है।

Volley, Android के लिए Google की HTTP library, यह सब handle करती है। आप एक request describe करते हैं, कौन सा URL, success या failure पर क्या करना है, और Volley इसे background में run करती है और आपको callback करती है। यह lesson Volley और इसका RequestQueue introduce करता है, तो एक API call करना manual thread juggling की बजाय कुछ clean lines बन जाता है।

Theory

RequestQueue और Request Types

Volley एक RequestQueue के around centre करता है: एक queue जो आपकी network requests को background threads पर run करती है और हर result main thread पर वापस deliver करती है। आप queue एक बार create करते हैं, फिर इसमें requests add करते हैं।

Volley अलग responses के लिए request types offer करती है: raw text के लिए StringRequest, और JSON के लिए JsonObjectRequest / JsonArrayRequest (जो पहले से एक JSONObject या JSONArray में parsed आती हैं)। आप एक request अपने method (GET, POST), URL, एक success listener (Response.Listener), और एक error listener (Response.ErrorListener) से build करते हैं, फिर इसे queue में add करते हैं। Volley बाकी करती है: schedule करना, UI thread से हटकर run करना, आपको callback करना।

Practical

JSON के लिए एक Volley Request

val queue = Volley.newRequestQueue(context)   // create the RequestQueue once

val request = JsonObjectRequest(
    Request.Method.GET, "https://api.example.com/events", null,
    { response ->                 // success: response is a JSONObject
        val events = response.getJSONArray("events")
        // parse and show them
    },
    { error ->                    // failure
        // show an error message
    }
)

queue.add(request)   // Volley runs it off the UI thread and calls back

Formula

Volley आपके लिए Threading Handle करती है

Big win यह है Volley automatically network work को main (UI) thread से हटकर रखती है। Android पर, main thread पर network calls करना interface freeze कर देता (और Android इसे forbid करता है), तो work को background में run करना पड़ता है, और Volley इसे manage करती है, plus scheduling, prioritising, और retries।

आपके success और error listeners, हालाँकि, main thread पर run होते हैं, तो आप इनमें safely UI update कर सकते हैं। तो आपको correct, non-blocking networking मिलती है बिना खुद कोई thread-handling code लिखे। Request और callbacks describe कीजिए; Volley इसे properly run करती है। यही वजह है Volley जैसी libraries hand-rolled networking पर preferred होती हैं।

Quiz

Volley का RequestQueue आपके लिए क्या करता है?

  1. यह app का data device पर permanently store करता है
  2. यह network requests को background threads पर (UI thread से हटकर) run करता है और results callbacks के through वापस deliver करता है, threading आपके लिए handle करते हुए
  3. यह app के layouts design करता है
  4. यह एक internet connection की ज़रूरत को replace करता है
Show the answer

यह network requests को background threads पर (UI thread से हटकर) run करता है और results callbacks के through वापस deliver करता है, threading आपके लिए handle करते हुए

Volley का RequestQueue आपकी network requests को background threads पर run करता है, तो ये UI को block नहीं करतीं, और results आपके success/error callbacks (main thread पर) के through वापस deliver करता है, threading और scheduling आपके लिए handle करते हुए। Option A wrong है: Volley networking करती है, permanent local storage नहीं (वह एक database का काम है)। Option C wrong है: layouts Android के UI tools में build होते हैं, Volley में नहीं। Option D wrong है: Volley को API तक पहुँचने के लिए अभी भी internet चाहिए; यह requests manage करती है, यह connection की ज़रूरत नहीं हटाती। Benefit है minimal code के साथ correct, non-blocking networking।

Think first

Volley इस्तेमाल करने की बजाय Network Code हाथ से क्यों नहीं लिखें?

आप एक connection open कर सकते हैं और response खुद पढ़ सकते हैं। Volley जैसी एक library इस्तेमाल क्यों करें? फिर tap कीजिए।

Show the answer

क्योंकि correct Android networking में बहुत सारा subtle, repetitive, error-prone work involve होता है, threading, scheduling, error handling, retries, response parsing, जिसे Volley जैसी library पहले से अच्छी तरह solve कर चुकी है, तो इसे हाथ से लिखना effort waste करता है और bugs invite करता है। सोचिए raw networking को क्या चाहिए: आपको request एक BACKGROUND thread पर run करनी पड़ती है (कभी UI thread पर नहीं, नहीं तो app freeze हो जाती है और Android इसे kill कर सकता है), फिर UI safely update करने के लिए result को main thread पर वापस marshal करना पड़ता है, timeouts और connection errors gracefully handle करने पड़ते हैं, शायद failed requests retry करनी पड़ती हैं, device को overwhelm किए बिना कई concurrent requests manage करनी पड़ती हैं, और response parse करना पड़ता है। हर request के लिए इन सब को हाथ से सही करना tedious है और botch करना easy है, background thread से एक misplaced UI update, एक leaked thread, एक unhandled error, और हर mistake एक crash या freeze है। Volley correct solution package करती है: आप बस request describe करते हैं (method, URL, listeners) और इसे queue में add करते हैं, और Volley threading, scheduling, prioritisation, और retries handle करती है, आपके listeners को main thread पर वापस call करते हुए तो UI updates safe हैं। इसका मतलब है कम code, कम bugs, और consistent behaviour, और यह आपको WHAT आप fetch कर रहे हैं उस पर focus करने देता है, HOW की plumbing पर नहीं। यही good libraries की general value है: वे hard, common problems encapsulate करती हैं तो आपको उन्हें फिर से solve नहीं करना पड़ता। Android networking के लिए, Volley (और Retrofit जैसे alternatives) exactly इसी reason के लिए professional default हैं। Tricky plumbing re-implement करने की बजाय एक solved solution reuse कीजिए।

Summary

Key takeaways

  • Volley Android के लिए Google की HTTP library है जो network calls simplify करती है।
  • यह एक RequestQueue के around centre करती है जो requests को background threads पर run करती है और results main thread पर वापस deliver करती है।
  • Request types: StringRequest (raw text), JsonObjectRequest और JsonArrayRequest (पहले से-parsed JSON)।
  • आप एक request एक method, URL, एक success listener (Response.Listener), और एक error listener से build करते हैं, फिर इसे queue में add करते हैं।
  • Volley automatically network work को UI thread से हटकर रखती है (तो app freeze नहीं होती) और safe UI updates के लिए main thread पर आपके callbacks run करती है।
  • एक library इस्तेमाल करना threading, scheduling, retries, और error handling के error-prone manual work से बचाता है।
  • Memory hook: एक RequestQueue create कीजिए, success/error listeners के साथ एक request add कीजिए, Volley इसे UI thread से हटकर run करती है और आपको callback करती है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Working with Data and API in Android

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Introduction to Volley libraries · Advance Mobile Application Development - II (Major-15-02) · Gri-Learn