KH_AJU KH_AJU - 8 months ago 37
Android Question

Android volley response data not loading to view

Am experiencing a strange problem here, I got the GET response using volley and loading the data to the recycler view; the data however not reflecting in view unless I lock and unlock the phone once, while the application is running. Then I tried the same response locally storing into asset folder,its working superfine. Am not getting any clue on this, the GET responses are showing in the logger; I've tried the same with Listview too, but the result was same always. Please help me on this....

protected void onCreate(Bundle savedInstanceState) {

newsFeed = engine();

rv = (RecyclerView) findViewById(;
llm = new LinearLayoutManager(this);



private void initializeAdapter(ArrayList<newsItem> newsFeed) {
context = getApplicationContext();
NewsViewAdapter adapter = new NewsViewAdapter(context, newsFeed);

private ArrayList<newsItem> engine() {
//String res=loadJSONFromAsset();
RequestQueue queue = Volley.newRequestQueue(this);
final ArrayList<newsItem> reqs = new ArrayList<>();
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
// longInfo(response.toString());
JSONObject newsItems = response.getJSONObject("response");
JSONArray webNews = newsItems.getJSONArray("results");
int a = webNews.length();

for (int i = 0; i < a; i++) {
JSONObject temp = webNews.getJSONObject(i);
String title = temp.getString("webTitle");
Log.d("mytitile", title);
// String time = temp.getString("time");
String date = temp.getString("webPublicationDate");
String content = temp.getString("id");
Log.d("myContent", content);
String link = temp.getString("webUrl");
JSONObject temp_img = temp.getJSONObject("fields");
String image = temp_img.getString("thumbnail");
Log.d("imageUrl", image);
reqs.add(new newsItem(title, content, date, link, image));

} catch (JSONException e) {
Log.i("myTag", e.toString());

new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());

myReq.setRetryPolicy(new DefaultRetryPolicy(
10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT

Answer Source

Use this:

in your fields:

private ArrayList<newsItem> items = new ArrayList<>();
private NewsViewAdapter adapter;

in your initializeAdapter:

 adapter = new NewsViewAdapter(context, items);

and in your onResponse, instead of reqs.add

 items.add(new newsItem(parameters..));

after your for loop in the onResponse, you call


and your good to go