Fetch data from Api in android using Volley library
what we learn
recyclerview in android: we can fetch data from android and show the data in recyclerview.
add this two library in build gradle
implementation 'com.android.volley:volley:1.2.1'
implementation 'org.jsoup:jsoup:1.13.1'
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
// creating a new json object and
// getting each object from our json array.
try {
// we are getting each json object.
JSONObject responseObj = response.getJSONObject(i);
// now we get our response from API in json object format.
// in below line we are extracting a string with
// its key value from our json object.
// similarly we are extracting all the strings from our json object.
String courseName = responseObj.getString("courseName");
String courseTracks = responseObj.getString("courseTracks");
String courseMode = responseObj.getString("courseMode");
String courseImageURL = responseObj.getString("courseimg");
list.add(new dataModels(courseName,courseImageURL,courseMode,courseTracks));
// binding.text.setText(courseName);
} catch (JSONException e) {
e.printStackTrace();
}
}
dataAdapters adapters = new dataAdapters(list,getApplicationContext());
binding.recyclerview.setAdapter(adapters);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
binding.recyclerview.setLayoutManager(linearLayoutManager);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Fail to get the data..", Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonArrayRequest);
project demo
// Instantiate the RequestQueue. | |
queue = Volley.newRequestQueue(this); | |
String url = Constants.GET_CATEGORIES_URL; | |
// Request a string response from the provided URL. | |
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
try { | |
JSONObject jsonObject = new JSONObject(response); | |
if(jsonObject.get("status").equals("success")){ | |
JSONArray data = jsonObject.getJSONArray("categories"); | |
for(int i=0; i<data.length(); i++){ | |
JSONObject d1 = data.getJSONObject(i); | |
CategoriesModels category = new CategoriesModels( | |
d1.getInt("id"), | |
d1.getString("name"), | |
d1.getString("brief"), | |
Constants.CATEGORIES_IMAGE_URL+d1.getString("icon") | |
); | |
clist.add(category); | |
cadapters.notifyDataSetChanged(); | |
} | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
Toast.makeText(MainActivity.this, "That didn't work!", Toast.LENGTH_SHORT).show(); | |
} | |
}); |
Comments
Post a Comment