0

I am making a calories counter application in android studio. I have created all the adapter and layouts everything but i am struck at one place I am trying to use rapid APIs Food Calorie Data Search API and it gives a URL and a key I don't know where to add that key.

 private void parseJSON() {
 String url="edamam-edamam-nutrition-analysis.p.rapidapi.com/api/nutrition-data?key=ingr=1%20large%20apple";
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response)
            {
                JSONArray jsonArray = null;
                try {
                    jsonArray = response.getJSONArray("hits");

                    for (int i = 0; i < jsonArray.length(); i++)
                    {
                        JSONObject hit = jsonArray.getJSONObject(i);
                        String name = hit.getString("name");
                        int protein = hit.getInt("protein");
                        int carbs = hit.getInt("carbs");
                        int fats = hit.getInt("fats");
                        int calories = hit.getInt("calories");

                        mExampleList.add(new element_item(name,protein,carbs,fats,calories));
                    }

                    mExampleAdapter = new elementAdaptor(MainActivity.this, mExampleList);
                    mRecyclerView.setAdapter(mExampleAdapter);

                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mRequestQueue.add(jsonObjectRequest);
    }
Linda Paiste
  • 38,446
  • 6
  • 64
  • 102

1 Answers1

0

You should add the respective headers to it after the error listener

        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {....}, new Response.ErrorListener() {....}) {

    @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  params = new HashMap<>();
            params.put("X-RapidAPI-Host", "Your api Host");
            params.put("X-RapidAPI-Key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");   //changedkey
            return params;
       }};