There is a task to download data from api, process and transfer the list to mainActivity. The first time I load the data, everything works fine - when the second run, asyncTask constantly returns null. The stream is triggered (in the doinBackground it generates new data, but does not send it to the activation), how to fix it?

@Override public void onClick(View v) { try { switch (v.getId()){ case R.id.btnStartSearch: String query_str = etSearch.getText().toString(); if(!query_str.isEmpty()){ List<ContactItem> list = new ArrayList<>(); LoadAPI loadAPI = new LoadAPI(); loadAPI.execute(query_str); list = loadAPI.get(); if(list == null){ Toast.makeText(this, "Введіть коректніші дані пошуку!", Toast.LENGTH_SHORT).show(); } else { initRecycler(list); Log.println(Log.INFO, "api", "list size - " + list.size()); } //loadAPI.cancel(true); //loadAPI.execute("close", loadAPI.toString()); //loadAPI.execute(); } else { Toast.makeText(this, "Помилка! Введіть данні пошуку!", Toast.LENGTH_SHORT).show(); } break; } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } 

here's the stream code

 public class LoadAPI extends AsyncTask<String, Void, List<ContactItem>> { private List<ContactItem> contactItemList; @Override protected List<ContactItem> doInBackground(String... strings) { try { String query = URLEncoder.encode(strings[0], "UTF-8"); String link_query = "https://public-api.nazk.gov.ua/v1/declaration/? q=" + query; Log.println(Log.INFO, "api", link_query); Document document = Jsoup.connect(link_query).ignoreContentType(true).get(); String result = document.text(); //dell Log.println(Log.INFO, "api", result); JSONObject jsonObject = new JSONObject(result); JSONArray arrayTovars = jsonObject.getJSONArray("items"); int count = jsonObject.getJSONObject("page").getInt("totalItems"); if (count < 500) { contactItemList = new ArrayList<>(); Log.println(Log.INFO, "api", "count: " + count); for (int i = 0; i < arrayTovars.length(); i++) { ContactItem contact = new ContactItem(); JSONObject item = arrayTovars.getJSONObject(i); contact.setId(item.getString("id")); contact.setFirst_name(item.getString("firstname")); contact.setLast_name(item.getString("lastname")); contact.setMisce_rob(item.getString("placeOfWork")); contact.setPosada(item.getString("position")); contact.setLink_pdf(item.getString("linkPDF")); contact.setLink_pdf(item.getString("linkPDF")); contactItemList.add(contact); } } else { return null; } //String res = contactItemList.get(0).getLast_name() + " " + contactItemList.get(0).getFirst_name(); //Log.println(Log.INFO, "api", res); return contactItemList; } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(List<ContactItem> list) { super.onPostExecute(list); } } 

Thank you so much for the help in advance.

  • Maybe count >= 500 ? - woesss
  • yes no, there when I enter the data again returns 22, but still null in the activation - Andrey Yatsenko
  • Then the exception - look in the logs IOException and JSONException - woesss
  • You were right - it was in the logs that one of the fields was said to be empty, and for some reason it returned null for some reason. Thanks a lot !! :) - Andrey Yatsenko

0