API请求适用于邮递员,但凌空时间超时

时间:2017-11-17 17:55:16

标签: android api android-volley get-request

所以我在spoonucalur中使用API​​请求,这是一个提供食谱的配方api。我在postman里面尝试了get请求,但它运行正常。但是,它在我的应用程序中工作非常不稳定。我正在使用android studio,使用volley来处理get请求。

 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);

有时它会返回正确的信息,但有时它会说timeOutError。它根据我的要求在邮递员内部返回相当快,因此我不知道为什么它现在不起作用。

以下是api链接:https://market.mashape.com/spoonacular/recipe-food-nutrition 以下是我正在使用的获取请求:https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByNutrients?maxCalories=4000&number=1&offset=0&random=true

1 个答案:

答案 0 :(得分:0)

更改RetryPolicy上的Request以允许更长的超时时间。

实施例

final int TIMEOUT_MS = 10000;
getRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy. DEFAULT_BACKOFF_MULT);
...
相关问题