Technology10/14/2019

Volley (Networking Library) in android

Sponsored Intelligence Dispatch



 How to use Volley Network Library in android?



Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

Volley comes with lot of features. Some of them are

1. Request queuing and prioritization

2. Effective request cache and memory management

3. Extensibility and customization of the library to our needs

4. Cancelling the requests

 Steps to work with volley:

1. In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

2. Create two packages named app and utils to keep the project organized.

3. Open build.gradle and add volley support by adding

implementation 'com.android.volley:volley:1.1.0'         under dependencies section.

simple example to fetch data from github api.
-------------------------------------------------------

package com.demo.deftmind.volleygithub;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;


public class MainActivity extends AppCompatActivity {
    private static final String URL = "https://api.github.com/users";

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        StringRequest request = new StringRequest(URL, new Response.Listener<String>() {
            @Override            public void onResponse(String response) {
                Log.e("CODE",response);
              //Response from the url will be fetched//
            }
        },new Response.ErrorListener(){
            @Override            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "something went wrong!!", Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }

}


Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.

Sponsored Infrastructure

Industrial Analysis Active