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 = "https://www.google.com/search?q=Android+GET+request";
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.
        }
    }, 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.
        }
    });

ExampleRequestQueue.add(ExampleStringRequest);

The String ‘response’ contains the server’s response. You can test it by printing response.substring(0,500) to the screen.

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.

How To Send A JsonObjectRequest From Android (Send GET Request For JSON Data)

Another type of GET request you’re likely to need on Android is the JsonObjectRequest. A JsonObjectRequest enables you to send an HTTP request for JSON objects. The structure of a JsonObjectRequest is very similar to that of a StringRequest, which is designed for strings.

String url = "http://exampleserver.com/route";

RequestQueue ExampleRequestQueue = Volley.newRequestQueue(this);
JsonObjectRequest ExampleRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setText(response.toString());
    }

}, 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.
    }
});
ExampleRequestQueue.add(ExampleRequest);

Update For 2020

If you are using a newer version of Android Studio (greater than version 4, for example), you can just add the dependency to your projects build.gradle file with one line of code, as shown below (then follow the same instructions above to import it and create the GET request).

Adding Volley as a dependency to build.gradle
Adding the Volley networking library as a dependency to build.gradle in Android Studio 4.01.

2 COMMENTS