• Register
294 points
‚óŹ3

How call REST Services in Android Application

 
Let’s start this project by creating a new project in Android Studios and name it *VolleyTutorial*.
 
Including the dependencies for the following in your build.gradle (Module:app).
 
Add Volley dependency :-
implementation 'com.android.volley:volley:1.1.1’

Add GSON dependency :- 
implementation 'com.google.code.gson:gson:2.8.5’


And now sync your project woth Gradle.



This is the open source API hosted whihc we will use for this tutorial :- https://reqres.in/api/users/2. You will get response like as below.

{
   "data":{
      "id":2,
      "email":"janet.weaver@reqres.in",
      "first_name":"Janet",
      "last_name":"Weaver",
      "avatar":"https://reqres.in/img/faces/2-image.jpg"
   },
   "support":{
      "url":"https://reqres.in/#support-heading",
      "text":"To keep ReqRes free, contributions towards server costs are appreciated!"
   }
}

You will need to make a model which will handle above response :-

ResponseModel.java

public class ResponseModel{

private Data data;
private Support support;

private class Data{

private int id;
private String email;
private String first_name;
private String last_name;
private String avatar;

public String getEmail(){
return email;
}

}

private class Support{

private String url;
private String text;

public String getUrl(){
return url;
}

}

// Add getters and setters to get any variables which is private 

public String getEmail(){
return data.getEmail();
}

}
Now you will make a StringRequest via Volley and will add into RequestQueue and then we will parse it to process our next action.

Make one method named as a callRestServices() so we can manage it differently. And add below line.
public static final String URL = "https://reqres.in/api/users/2";


StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        RestModel restRes = gson.fromJson(response,RestModel.class);

        Log.d("Rest Service Response --->>> ",restRes.getEmail());

  }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("MainActivity.class", "onErrorResponse: " + error.getMessage());
    }
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Now call this method inside createView() Method like as below :

public class MainActivity extends AppCompatActivity {

    public static final String URL = "https://api.github.com/users";
    RecyclerView recyclerView;

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

       your_rest_service_method();

    }

}

And now add Internet permission for calling rest service in your manifest file.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

We are all set now run your application and check log you will get USer’s email id in response.




 

2 Comments

Comment:

thank you for sharing this.
Today you have taught me something new. Great job. Continue to post :)