Breaking

Post Top Ad

Your Ad Spot

Tuesday, 7 May 2019

Volley library in Android

Introduction to Volley

  • Volley is an HTTP library for Android that makes networking for Android apps easier and more importantly faster.
  • Volley is available through the AOSP(Android Open Source Project) repository.
  • Volley is developed by Google and introduced in Google I/O 2013.

Advantages of Volley

  • Supports for request prioritization.
  • Automatic scheduling of network requests.
  • Allows multiple concurrent network connections.
  • You can cancel a request or block of request.

Volley vs Retrofit

This is one of the hottest topics in the Android Development field of whether to use Volley or Retrofit. Well as the title suggest, the blog is going to be on Volley not on Volley and Retrofit comparison. So, I am just going to discuss a few points that Volley has and Retrofit doesn't.

  • Volley has Cashing mechanism which Retrofit doesn't have.
  • Volley has a Retry Policy which is also missing in Retrofit.
  • Volley has support to handle images which are missing in Retrofit for which it makes use of Glide, Picasso, Universal Image loader, etc.
  • Now, here comes the best thing that Retrofit has and Volley doesn't is - conversion of JSON data to Java Objects which sometimes becomes the most tedious task for programmers if the JSON data is very complex(going into JSON object again going to another JSONArray and then collecting the data). This can easily be done with the help of GSON, Jackson, MOSHI which is used with Retrofit and not Volley. 

Let's now jump to the code.

Before you start writing the code for Volley. Since it is a library it must be added first in order to use it. So, first, go to Build.gradle and add this dependency.

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


This is a simple volley code that you can use to get data from the Server.

private void getData() { showpDialog(); RequestQueue queue = Volley.newRequestQueue(mContext); StringRequest postRequest = new StringRequest(Request.Method.POST, "Your URL Here", new Response.Listener<String>() { @Override public void onResponse(String response) { if (response != null) { try { hidepDialog(); JSONObject emp_details=new JSONObject(response); JSONArray emp_array=emp_details.getJSONArray("contacts"); for(int i=0;i<emp_array.length();i++) { JSONObject one_emp=emp_array.getJSONObject(i); String name=one_emp.getString("name"); String email=one_emp.getString("email"); JSONObject phone=one_emp.getJSONObject("phone"); String contact=phone.getString("mobile"); Emp_Details_model emp=new Emp_Details_model(name,email,contact); emp_details_list.add(emp); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(mContext, "Result not found", Toast.LENGTH_LONG).show(); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { hidepDialog(); Toast.makeText(mContext, "Result not found", Toast.LENGTH_LONG).show(); } }); postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); queue.add(postRequest); }

This is a simple volley code helps you send data onto the Server. private void sendData() { getData_you_want_to_send(); progressDialog.show(); RequestQueue queue = Volley.newRequestQueue(mContext); StringRequest postRequest = new StringRequest(Request.Method.POST, "YOUR URL HERE", new Response.Listener<String>() { @Override public void onResponse(String response) { if (response != null) { try { // Response will be here. } } catch (Exception e) { e.printStackTrace(); FancyToast.makeText(mContext, "Result not found", FancyToast.LENGTH_LONG, FancyToast.CONFUSING, false).show(); } } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Error response will be here... } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); // This block is for sending data onto the Server. params.put("phone_no", "xxxxxxxxxx"); params.put("imei", "xxxxxxxxxx"); return params; } }; postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); queue.add(postRequest); }





No comments:

Post a Comment