Как парсить ссылку, только если из поиска пришло значение и оно не пустое? Проблема в том, что даже ссылка без значения в конце дает результат
https://api.edamam.com/api/food-database/v2/parser...
А мне нужно отображать значения только если в конце идет значение из searchView, как это правильно организовать? Я пробовал это так но это не работает!
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";
private static String END_URL = "&app_key=5f7521ffeefe491b936cea6271e13d3d&ingr=";
private static String SEARCH_TEXT = null;
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;
SEARCH_TEXT = END_URL + Objects.requireNonNull(binding.searchs.getText()).toString();
try {
url = new URL(PARSE_URL + SEARCH_TEXT);
Log.i("url", String.valueOf(SEARCH_TEXT));
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("hints");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hintObject = jsonArray.getJSONObject(i);
JSONObject foodObject = hintObject.getJSONObject("food");
String label = foodObject.getString("label");
String image = foodObject.getString("image");
JSONObject nutrientsObject = foodObject.getJSONObject("nutrients");
String enercKcal = nutrientsObject.getString("ENERC_KCAL");
String procnt = nutrientsObject.getString("PROCNT");
String fat = nutrientsObject.getString("FAT");
String chocdf = nutrientsObject.getString("CHOCDF");
String fibtg = nutrientsObject.getString("FIBTG");
FoodModels model = new FoodModels();
model.setName(label);
model.setImg(image);
model.setKcal(enercKcal);
model.setProcent(procnt);
model.setFat(fat);
model.setChocdf(chocdf);
model.setFidtg(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.setHasFixedSize(true);
binding.recyclerMain.setAdapter(categoryAdapter);
binding.recyclerMain.setLayoutManager(new LinearLayoutManager(getContext()));
}
}