1. Initialize your adapter
and set it to RecyclerView
from onCreate()
method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
listcollege = new ArrayList<>();
adapter = new CardAdapter(this, listcollege);
recyclerView.setAdapter(adapter);
getData();
}
2. Instead of JsonArrayRequest
, try using JsonObjectRequest
:
//Creating a json object request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Config.DATA_URL,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Dismissing progress dialog
Toast.makeText(MainActivity.this, "sachin", Toast.LENGTH_SHORT).show();
loading.dismiss();
// College
JSONArray jsonArrayCollege = response.getJSONArray("College");
// calling method to parse json array
parseData(jsonArrayCollege);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonObjectRequest);
3. Update parseData()
method as below:
public void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
College college = new College();
JSONObject json = null;
try {
json = array.getJSONObject(i);
college.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
String s = (json.getString(Config.TAG_NAME));
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
college.setName(s);
} catch (JSONException e) {
e.printStackTrace();
}
listcollege.add(college);
}
adapter.notifyDataSetChanged();
}
Hope this will help~
0
solved No response from jsonArray request [closed]