По какой то причине у меня не выводится нужный мне список.
Вот полностью код, в нем ошибок нет, так как с другим апи работает нормально.
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("food");
for(int i = 0 ; i < jsonArray.length() ; i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
FoodModels model = new FoodModels();
model.setName(jsonObject1.getString("name"));
model.setImg(jsonObject1.getString("img"));
model.setKcal(jsonObject1.getString("kcal"));
model.setProcent(jsonObject1.getString("procent"));
model.setFat(jsonObject1.getString("fat"));
model.setChocdf(jsonObject1.getString("chocdf"));
model.setFidtg(jsonObject1.getString("fidtg"));
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.setHasFixedSize(true);
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"
},