I have a REST method that returns JSON object,the JSON file size almost weighs 7MB and has almost 4600 JSON object. I am unable to parse the entire data at a time into recyclerView as it causes OutOfMemory Exception.
I tried to implement it in 2 ways. One is using http://loopj.com/android-async-http/ but this method is not feasible to large chunk of data. The second method was done using GSON and Volley. Using this method i am able to get the response much faster from the REST method but i'm unable to populate it into recyclerView. Please do check out the code that i used.
The problem that i'm facing here is that recyclerview shows null values for ROLL_NUM, CURRENT_CLASS, STUDENT_NAME
sample JSON response: {"RestResult":[{""ROLL_NUM":7071,"CURRENT_CLASS":"Grade LKG C","STUDENT_NAME":"A. VEDANJALI AKULA VEDANJALI"}
ListItem.class
String STUDENT_NAME;
String CURRENT_CLASS;
String ROLL_NUMBER;
public String getSTUDENT_NAME() {
return STUDENT_NAME;
}
public String getCURRENT_CLASS() {
return CURRENT_CLASS;
}
public String getROLL_NUMBER() {
return ROLL_NUMBER;
}
public GetSet(String STUDENT_NAME, String CURRENT_CLASS, String ROLL_NUMBER) {
this.STUDENT_NAME = STUDENT_NAME;
this.CURRENT_CLASS = CURRENT_CLASS;
this.ROLL_NUMBER = ROLL_NUMBER;
}
RecyclerAdapter.class
private List<GetSet> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<GetSet> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.songTitle.setText("Student Name: " + itemList.get(position).getSTUDENT_NAME());
holder.songYear.setText("Current Class: " + itemList.get(position).getCURRENT_CLASS());
holder.songAuthor.setText("Roll Number: " + itemList.get(position).getROLL_NUMBER());
}
@Override
public int getItemCount() {
return this.itemList.size();
}
ActivityMain.class
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://webservices.educatemax.com/vidyaMandir.svc/RetrieveStudentsList?iGroupID=1&iSchoolID=1&iBoardID=1&iClassID=1";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.hide();
Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<GetSet> posts = new ArrayList<>();
posts = Arrays.asList(mGson.fromJson(response, GetSet.class));
adapter = new RecyclerViewAdapter(ActivityMain.this, posts);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);