How To Send A GET Request In Android

Android Tutorials

Volley, a networking library developed by Google, enables you to send ‘GET’ requests from your Android-powered device with very few lines of code. I would recommend using Android Studio, as it can accelerate the development process by generating some of the code for you (unless you’re implementing a custom request).

Android Studio screenshot

While there are many ways to send GET requests using Volley, this article explains the use of the StringRequest method. If your GET request will contain JSON data, you should create a JsonObjectRequest instead. If you need to send a JSON array in your request, use a JsonArrayRequest. Bear in mind that the Volley library is built for smaller downloads (such as text).

First, you need to add the Volley library to your project using the ‘library dependency’ option. ‘Dependency’ is a widely-used term to refer to a library that apps depend on.

Next, add the necessary imports to the activity that the GET request will be sent from, as shown below.

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;

import java.util.HashMap;
import java.util.Map;

You may now start writing your GET request!

Create a RequestQueue:

RequestQueue ExampleRequestQueue = Volley.newRequestQueue(this);

Create a StringRequest (remember to add the ‘http’ prefix) named ExampleStringRequest:

String url = "http://yourwebsite.com/path";
StringRequest ExampleStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //This code is executed if the server responds, whether or not the response contains data.
            //The String 'response' contains the server's response. 
        }
    }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {
            //This code is executed if there is an error.
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
            return MyData;
        }
    };
}

ExampleRequestQueue.add(ExampleStringRequest);

If you’d like to send a POST request instead, you could change the ‘.GET’ in the Request.Method.GET parameter to ‘.POST’ instead.  Ensure that your back-end is configured to listen for GET requests in this case. If you’re sending a POST request, you would have to configure your back-end to receive a POST request instead, otherwise it would fail.

Learn how to send POST requests in Android.

Be sure to add the ‘INTERNET’ permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Voila! You’re done.

Facebook Comments

comments

2 COMMENTS