Skip to content Skip to sidebar Skip to footer

Why Is This Skipping The Onresponse Method?

I want to get data from JSON and then return it in an array, but the array always return with null, because my program won't go into the onResponse method. I want to show this data

Solution 1:

Your code is wrong in the logical sense. Because the call to web service is asynchronous, so when you uses "return", the array is empty, when your data is in the result doesn't matter because you already loaded the RecyclerView. The solution will be that you implement a callback function or use notifydatasetchanged in the RecyclerView.

Check the links.

Using callbacks in android

Using notifydatasetchanged

---------- notifydatasetchanged ----------

for (int i = 0; i < jsonArray.length(); i++){
  JSONObject jsonObject = jsonArray.getJSONObject(i);
  SzabadEuMusorok szabadEuMusorok = newSzabadEuMusorok();
  JSONArray elementTexts = jsonObject.getJSONArray("element_texts");

  JSONObject titleObject = elementTexts.getJSONObject(0);
  szabadEuMusorok.setTitile(titleObject.getString("text"));

  JSONObject subjectObject = elementTexts.getJSONObject(3);
  szabadEuMusorok.setSubject(subjectObject.getString("text"));

  JSONObject mainObject = jsonObject.getJSONObject("files");
  szabadEuMusorok.setVideoURL(mainObject.getString("url"));

  mSzabadEuMusoroks[i] = szabadEuMusorok;
}
//LINE ADDED-----------------------------------------------------
yourAdapterInTheRecyclerView.notifyDataSetChanged();
//LINE ADDED-----------------------------------------------------

Post a Comment for "Why Is This Skipping The Onresponse Method?"