Introduction to Volley libraries

Volley Android માટે Googleની HTTP library છે. તે network callsને main threadની બહાર ચલાવે છે, RequestQueue manage કરે છે અને callbacks દ્વારા results આપે છે, એટલે થોડા linesમાં API request કરી શકાય છે.

10 min read · 7 cards · 2 checks

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


Theory

Networking સરળ બનાવવું

Androidમાંથી APIને manually call કરવું દેખાવમાં સરળ લાગે છે, પરંતુ તેમાં ઘણી મુશ્કેલીઓ છે: network calls main threadની બહાર ચલાવવી પડે છે, નહીં તો app freeze થાય છે, અને results UI update કરવા માટે પાછા main thread પર લાવવા પડે છે. Threading જાતે manage કરવું error-prone છે.

Volley, Android માટે Googleની HTTP library, આ બધું handle કરે છે. તમે request, URL અને success અથવા failure વખતે શું કરવું તે describe કરો છો. Volley requestને backgroundમાં ચલાવે છે અને callback દ્વારા તમને result આપે છે. આ lesson Volley અને તેના RequestQueueનો પરિચય કરાવે છે, જેથી manual thread jugglingને બદલે API call થોડા clean linesમાં થઈ જાય.

Theory

RequestQueue અને request types

Volleyનું centre RequestQueue છે: તે network requestsને background threads પર ચલાવે છે અને દરેક resultને main thread પર પાછો deliver કરે છે. Queue એકવાર બનાવો અને તેમાં requests add કરો.

વિવિધ response માટે Volley અલગ request types આપે છે: raw text માટે StringRequest, અને JSON માટે JsonObjectRequest અથવા JsonArrayRequest. JSON requestsનો response પહેલેથી JSONObject અથવા JSONArray તરીકે parsed મળે છે. Request બનાવતી વખતે તેનો method (GET અથવા POST), URL, success listener (Response.Listener) અને error listener (Response.ErrorListener) આપો, પછી તેને queueમાં add કરો. Scheduling, background execution અને callback Volley સંભાળે છે.

Practical

Volleyથી JSON 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 કરે છે

મોટો લાભ એ છે કે Volley network workને આપમેળે main અથવા UI threadની બહાર ચલાવે છે. Androidમાં main thread પર network call કરવાથી interface freeze થાય છે અને તે મંજૂર નથી. Volley background execution ઉપરાંત scheduling, prioritising અને retries પણ manage કરે છે.

Success અને error listeners સામાન્ય રીતે main thread પર ચાલે છે, એટલે તેમાં UI safely update કરી શકાય છે. તમને thread-handling code લખવાની જરૂર નથી: request અને callbacks describe કરો, Volley તેને યોગ્ય રીતે run કરે છે. આ કારણે hand-written networking કરતાં Volley જેવી library વાપરવી વધુ યોગ્ય છે.

Quiz

Volleyનું RequestQueue તમારા માટે શું કરે છે?

  1. Appનો data device પર permanently store કરે છે
  2. Network requestsને background threads પર ચલાવે છે અને callbacks દ્વારા results પાછા આપે છે, એટલે threading handle કરે છે
  3. Appના layouts design કરે છે
  4. Internet connectionની જરૂર દૂર કરે છે
Show the answer

Network requestsને background threads પર ચલાવે છે અને callbacks દ્વારા results પાછા આપે છે, એટલે threading handle કરે છે

Volleyનું RequestQueue network requestsને background threads પર ચલાવે છે, જેથી UI block ન થાય, અને success અથવા error callbacks દ્વારા results પાછા આપે છે. Threading અને scheduling Volley handle કરે છે. Option A ખોટું છે: Volley networking માટે છે, permanent local storage માટે નહીં. Option C ખોટું છે: layouts Androidના UI toolsમાં બને છે. Option D ખોટું છે: API સુધી પહોંચવા Volleyને internet હજી પણ જોઈએ છે; તે requests manage કરે છે, connectionની જરૂર દૂર કરતું નથી. Main benefit ઓછા codeમાં correct, non-blocking networking છે.

Think first

Volleyને બદલે network code હાથથી શા માટે ન લખવું?

તમે connection ખોલીને response જાતે read કરી શકો છો. તો Volley જેવી library શા માટે વાપરવી? પછી tap.

Show the answer

કારણ કે Androidમાં correct networking માટે ઘણું repetitive અને error-prone કામ કરવું પડે છે: threading, scheduling, error handling, retries અને response parsing. Volleyએ આ common problems પહેલેથી solve કરી છે, તેથી તેને દરેક request માટે ફરી લખવું effort બગાડે છે અને bugsની શક્યતા વધારે છે.

Raw networkingમાં request BACKGROUND thread પર ચલાવવી પડે છે, નહીં તો UI freeze થાય અને Android appને unresponsive માની શકે. પછી result safely main thread પર પાછો લાવીને UI update કરવો પડે. Timeouts અને connection errors handle કરવા, failed requests retry કરવા, અનેક concurrent requests manage કરવા અને response parse કરવા પડે છે. નાની ભૂલ પણ crash, freeze, leaked thread અથવા unhandled error બનાવી શકે છે.

Volleyમાં તમે method, URL અને listeners સાથે request describe કરીને queueમાં add કરો છો. Volley threading, scheduling, prioritisation અને retries handle કરે છે અને listenersને main thread પર callback કરે છે, જેથી UI updates safe રહે. આથી code ઓછો, bugs ઓછા અને behaviour consistent રહે છે. તમે HOWનું plumbing ફરી બનાવવાને બદલે WHAT data fetch કરવું છે તેના પર focus કરી શકો છો. Good librariesનું general value પણ આ જ છે: difficult common problems માટે solved solution reuse કરો.

Summary

Key takeaways

  • Volley Android માટે Googleની HTTP library છે, જે network calls સરળ બનાવે છે.
  • તે RequestQueue પર આધારિત છે, જે requestsને background threads પર ચલાવે છે અને results main thread પર પાછા આપે છે.
  • Request types: raw text માટે StringRequest અને parsed JSON માટે JsonObjectRequest તથા JsonArrayRequest.
  • Requestમાં method, URL, success listener (Response.Listener) અને error listener આપીને તેને queueમાં add કરો.
  • Volley network workને UI threadથી દૂર રાખે છે અને callbacks main thread પર ચલાવે છે, તેથી UI safely update કરી શકાય છે.
  • Library વાપરવાથી threading, scheduling, retries અને error handlingનું મુશ્કેલ manual કામ ટળી જાય છે.
  • Memory hook: RequestQueue બનાવો, success/error listenersવાળી request add કરો; Volley backgroundમાં 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