In this post you’ll be learning about volley ,an android library and how to use it to fetch data from internet. It’s a library used for networking. Click this https://developer.android.com/training/volley to see what official android developers website tells about volley.  This is only a basic example of volley.

Follow these steps to setup the app for volley

  • After creating a empty project in Android Studio,go to the gradle       dependencies and add the volley dependencies from this link https://developer.android.com/training/volley
  • We are going to fetch a webpage on button press, for this we would need internet permissions. So under manifests define internet permission.
<uses-permission android:name="android.permission.INTERNET" />

Now lets dive into the coding part

Copy this into your activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.studio.volleyone.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

It would look something like this,

volley

Copy this into MainActivity.java

package com.example.studio.volleyone;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.android.volley.Request;
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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final String myURL = "http://www.facebook.com";

        Button myButton = (Button) findViewById(R.id.button);

        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

                StringRequest stringRequest = new StringRequest(Request.Method.POST, myURL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {
                            Log.i("PAGE", "WEB " + s);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.i("ERROR", "ERROR is " + volleyError);
                            }
                        }
                );

                requestQueue.add(stringRequest);
            }
        });



    }
}

In MainActivity.java, we are assigning a click listener on button and on button pressed ,it requests the webpage. So let’s see how the request is done.

An HTTP has many methods such as GET, PUT , CONNECT ,POST… , In example app the method  to request data from server is POST.

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

                StringRequest stringRequest = new StringRequest(Request.Method.POST, myURL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {
                            Log.i("PAGE", "WEB " + s);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.i("ERROR", "ERROR is " + volleyError);
                            }
                        }
                );

                requestQueue.add(stringRequest);
            }
        });

The above piece of code is to request, when button is pressed. Volley.newRequestQueue is assigned to an instance of RequestQueue in which all the requests are held in a queue. Then a StringRequest requests the url with the POST method and if it is successful then onResponse() is executed else onErrorResponse  is executed.

Now after running your app , click on the button and go to logcat and from dropbox choose info and type PAGE .If the request was successful then the xml format of web page could be seen in logcat else an error occurred while fetching web page ,in that case cross check your url. It would look something like this,

volley

Thus fetching a web page using volley is done in this way. This was just a basic example of volley ,there’s more to learn about volley. Visit developers official site or just keep calm and stay tuned with us ,We will post more advanced example on volley.

Thank you for reading this post. Follow us on Instagram and Facebook.


To know more tips and tricks you can follow articles on this category :
http://bunkerspoint.com/off-topic/

To know about web development and WordPress follow
http://bunkerspoint.com/web/

To know about Android development and Android Studio follow
http://bunkerspoint.com/android/

LEAVE A REPLY

Please enter your comment!
Please enter your name here