[英]how do i use a API from rapid code in android app using volley with keys
我正在 android 工作室制作卡路里计数器应用程序。 我已经创建了所有适配器和布局,但我在一个地方感到震惊,我正在尝试使用快速 API 食物卡路里数据搜索 API,它提供了 URL 和一个我不知道在哪里添加该密钥的密钥。
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);
}
您应该在错误侦听器之后添加相应的标头
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;
}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.