Volley第一次请求中的TIMEOUT错误

时间:2017-04-10 05:53:10

标签: android

我是android新手。我正在使用Volley来呼叫REST API网络服务。有一件事我弄清楚我第一次打电话REST API总是失败。它会抛出TIMEOUT错误。但是当我尝试重新运行它时,它可以工作。为什么我的第一次调用是超时错误我无法弄清楚。任何人都可以帮我处理这个问题。感谢。

我在下面添加了我的代码。

public void prepareDealsListData(final String location, final FragmentManager fragmentManager) {
    if (ConnectivityReceiver.isConnected()) {
        // show dialog to user
        // initialize the progress dialog and show it
        progressDialog = new ProgressDialog(_myActivity);
        progressDialog.setMessage("Loading....");
        progressDialog.show();

        Map<String, String> regParamMap = new HashMap<>();
        regParamMap.put("location", location);

        Uri.Builder builder = new Uri.Builder();
        Iterator entries = regParamMap.entrySet().iterator();

        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            builder.appendQueryParameter(entry.getKey().toString(), entry.getValue().toString());
            entries.remove();
        }

        final String requestBody = builder.build().getEncodedQuery();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_DEALS + "?" + requestBody, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();

                try {
                    GsonBuilder gsonb = new GsonBuilder();
                    Gson gson = gsonb.create();
                    deals = gson.fromJson(response.trim(), Deals.class);
                    // For first time load the offer fragment
                    loadFragment(fragmentManager, "OfferFragment", deals.getAllSpecial());
                } catch (IllegalStateException | JsonSyntaxException e) {
                    e.printStackTrace();
                    Log.e(TAG, "IllegalStateException | JsonSyntaxException");
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Log.d(TAG, error.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("location", location);
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", CONTENT_TYPE_JSON);
                return params;
            }

            @Override
            public String getBodyContentType() {
                return CONTENT_TYPE_JSON;
            }
        };

        stringRequest.setRetryPolicy(new DefaultRetryPolicy(TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(stringRequest, "Offer Lists");
    }
}

1 个答案:

答案 0 :(得分:0)

 public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    req.setRetryPolicy(
            new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 3,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    DebugLog.printLog(tag, "getRequest queue inside my application called" + req.toString() + "tag" + tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    addToRequestQueue(req, TAG);
}

@Alok,如果已经设置了重试策略,请检查您的应用类方法addToRequestQueue(),并再次使用上述代码进行设置。还要按照上述方法从应用程序类发出请求。有关更多信息,请查看此主题。 Volley Timeout error