Почему получаю пустой RecyclerView?
По какой то причине у меня не выводится нужный мне список.
Вот полностью код, в нем ошибок нет, так как с другим апи работает нормально.
public class HomeFragment extends Fragment implements FoodRecyclerViewInterface {
public HomeFragment() {
// Required empty public constructor
}
private FragmentHomeBinding binding;
private static String PARSE_URL = "https://api.edamam.com/api/food-database/v2/parser?app_id=e5bc806d&app_key=5f7521ffeefe491b936cea6271e13d3d&ingr=nut";
List<FoodModels> foodModelsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
foodModelsList = new ArrayList<>();
GetData getData = new GetData();
getData.execute();
return view;
}
@Override
public void onItemClick(int position) {
Intent i = new Intent(getActivity(), DetailActivity.class);
i.putExtra("name", foodModelsList.get(position).getName());
i.putExtra("img", foodModelsList.get(position).getImg());
i.putExtra("kcal", foodModelsList.get(position).getKcal());
i.putExtra("procent", foodModelsList.get(position).getProcent());
i.putExtra("fat", foodModelsList.get(position).getFat());
i.putExtra("chocdf", foodModelsList.get(position).getChocdf());
i.putExtra("fidtg", foodModelsList.get(position).getFidtg());
startActivity(i);
}
public class GetData extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
String current = "";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(PARSE_URL);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int data = isr.read();
while(data != -1) {
current += (char) data;
data = isr.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(urlConnection != null) {
urlConnection.disconnect();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return current;
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("parsed");
for(int i = 0 ; i < jsonArray.length() ; i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
FoodModels model = new FoodModels();
model.setName(jsonObject1.getString("label"));
model.setImg(jsonObject1.getString("image"));
model.setKcal(jsonObject1.getString("ENERC_KCAL"));
model.setProcent(jsonObject1.getString("PROCNT"));
model.setFat(jsonObject1.getString("FAT"));
model.setChocdf(jsonObject1.getString("CHOCDF"));
model.setFidtg(jsonObject1.getString("FIBTG"));
foodModelsList.add(model);
}
} catch (JSONException e) {
e.printStackTrace();
}
PutDataIntoRecyclerView(foodModelsList);
}
}
private void PutDataIntoRecyclerView(List<FoodModels> categoryList) {
FoodAdapter categoryAdapter = new FoodAdapter(getContext(), categoryList, this);
binding.recyclerMain.setAdapter(categoryAdapter);
binding.recyclerMain.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Возможно проблема в том что присутствуют вложенные массивы? Пример полученного ответ от апи
{
"text": "nut",
"parsed": [
{
"food": {
"foodId": "food_amqspy5ap567v6bun60usbgsaor7",
"label": "Nuts",
"knownAs": "nuts",
"nutrients": {
"ENERC_KCAL": 594,
"PROCNT": 17.3,
"FAT": 51.45,
"CHOCDF": 25.35,
"FIBTG": 9
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/e29/e2985d3585a6950c276e204ba7ba9b2e.jpg"
}
}
],
"hints": [
{
"food": {
"foodId": "food_amqspy5ap567v6bun60usbgsaor7",
"label": "Nuts",
"knownAs": "nuts",
"nutrients": {
"ENERC_KCAL": 594,
"PROCNT": 17.3,
"FAT": 51.45,
"CHOCDF": 25.35,
"FIBTG": 9
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/e29/e2985d3585a6950c276e204ba7ba9b2e.jpg"
},
Пробовал таким образом но все равно нечего не получаю
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray1 = jsonObject.getJSONArray("parsed");
for(int i = 0 ; i < jsonArray1.length() ; i++) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
JSONArray jsonArray2 = jsonObject1.getJSONArray("hints");
JSONObject jsonObject2 = jsonArray2.getJSONObject(i);
JSONArray jsonArray3 = jsonObject2.getJSONArray("food");
JSONObject jsonObject3 = jsonArray3.getJSONObject(i);
FoodModels model = new FoodModels();
model.setName(jsonObject3.getString("label"));
foodModelsList.add(model);
}
}
По идее ниже описанным способом я могу получить все label и image из объекта food который находится в hints но почему я не получаю вообще нечего?
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray1 = jsonObject.getJSONArray("hints");
for(int i = 0 ; i < jsonArray1.length() ; i++) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
JSONArray jsonArray2 = jsonObject1.getJSONArray("food");
JSONObject jsonObject2 = jsonArray2.getJSONObject(i);
FoodModels model = new FoodModels();
model.setName(jsonObject2.getString("label"));
foodModelsList.add(model);
}
}
json
"food": {
"foodId": "food_amqspy5ap567v6bun60usbgsaor7",
"label": "Nuts",
"knownAs": "nuts",
"nutrients": {
"ENERC_KCAL": 594,
"PROCNT": 17.3,
"FAT": 51.45,
"CHOCDF": 25.35,
"FIBTG": 9
},
"category": "Generic foods",
"categoryLabel": "food",
"image": "https://www.edamam.com/food-img/e29/e2985d3585a6950c276e204ba7ba9b2e.jpg"
},
"measures": [
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_unit",
"label": "Whole",
"weight": 2
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_pouch",
"label": "Pouch",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_serving",
"label": "Serving",
"weight": 34
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_nut",
"label": "Nut",
"weight": 2
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_bag",
"label": "Bag",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_container",
"label": "Container",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_package",
"label": "Package",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_packet",
"label": "Packet",
"weight": 113
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_gram",
"label": "Gram",
"weight": 1
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_ounce",
"label": "Ounce",
"weight": 28.349523125
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_pound",
"label": "Pound",
"weight": 453.59237
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_kilogram",
"label": "Kilogram",
"weight": 1000
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_handful",
"label": "Handful",
"weight": 17.125
},
{
"uri": "http://www.edamam.com/ontologies/edamam.owl#Measure_cup",
"label": "Cup",
"weight": 137
}
]
}