API makes our life way easier than we can think of. While working with android studio and with java, you might probably wish how to access data from an API.
In this story, I will show you how you can get the data from an API. Just to make things simple, I will use a fake API which you probably need to know about, because it is very important to learn with fake API and then get into the real world.
So, the API which we will be using is Json Placeholder, an amazing website for providing a fake API. we will also use Volley Library to make things simple.
In this story, I will show you how to access JSON objects and JSON arrays. in the first section, I will show you how to access JSON Object from a remote api and then in the second section, I will show you how to access JSON array. so actually, there are two ways for the two.
Accessing JSON Object with Android Studio (Java):- To access Json Object it is simple you can just get it in the following way. Look out into the documented code and you will understand the case scenario.
package com.alixaprodev.apistudy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
// Creating the queue for the request this way it will be good if there is a prblem with
// the device or there are other activities going on with the network. the request will
// be in the queue. we will add our requests into the queue.
private RequestQueue requestQueue ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String apiURL = "https://jsonplaceholder.typicode.com/todos/1";
requestQueue = Volley.newRequestQueue(this);
// JSON Object Request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, apiURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("JSON",response + " ");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
}
Accessing JSON Array with Android Studio (Java):- To access Json Array it is simple you can just get it in the following way. Look out into the documented code and you will understand the case scenario.
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET,
"https://jsonplaceholder.typicode.com/todos/",
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i =0; i<response.length();i++){
try {
Log.d("response "+ i +":", response.getJSONObject(i).toString() );
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(arrayRequest);
}
}
That's it. Have a look at the documentation of the Volley Library and the JSON placeholder documentation as well.
Still, if you have any questions, please let me know in the comment sections or reach me at (hazrataliuop@gmail.com). Hire me for your Android Project at Fiverr.
About Me:
This is Hazrat Ali. I am a Software Developer by Profession for the last 3 years. I have a wide range of experience in Java Programming, Python Programming, Android Application Development, and Web Technologies. I have Many Happy Clients across the Globe.
Contact me:
email: hazratali0@gmail.com
Linkedin: linkedin.com/hazratali