2

So I was using API request in spoonucalur, which is a recipe api that provide recipe. I tried the get request inside postman and it works fine. However, it works so unstable in my app. I am using android studio, using volley for the library to handle the get request.

 JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            callbackHelper.onSuccess(response.getJSONObject(0));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("error", error.toString());
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("X-Mashape-Key", currentAPI);
                params.put("Accept", "application/json");
                return params;
            }
        };
        Log.d("request", "starting");
        queue.add(getRequest);

Sometimes it return me the correct information, but sometimes it will just say timeOutError. It returns pretty fast inside postman for my request, therefore I have no idea why it wouldn't work for now.

Here is the api link : https://market.mashape.com/spoonacular/recipe-food-nutrition Here is the get request that I'm using : https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByNutrients?maxCalories=4000&number=1&offset=0&random=true

Wal Wai
  • 29
  • 2

1 Answers1

0

Change the RetryPolicy on your Request to allow for longer timeouts.

Ex.

final int TIMEOUT_MS = 10000;
getRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy. DEFAULT_BACKOFF_MULT);
...
Submersed
  • 8,810
  • 2
  • 30
  • 38