0

I am new to android. I am using Volley to call REST API webservices. One thing I have figure out that my first call to REST API always fails. It throws TIMEOUT error. But when I try to re-run it, it works. Why my first call is getting timeout error I am unable to figure out. Can anyone help me in dealing this issue. Thanks.

I have added my code below.

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");
    }
}
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
Alok
  • 297
  • 3
  • 13
  • Maybe it is taking time to connect, do this network operation in background, and just when you enter `prepareDealsListData()` call `Thread.sleep(2000);` for 2 seconds wait, let it connect. See if this helps. – Talha Apr 10 '17 at 05:57
  • Can you plz give me an example of doing Thread.sleep(). – Alok Apr 10 '17 at 06:03
  • how much time you have set to connection. – DkThakur Apr 10 '17 at 06:03
  • Earlier it was 5000ms then I have increased it to 50000ms – Alok Apr 10 '17 at 06:06
  • I didnt noticed at first though, you dont need to make a `sleep()` call as Volley is already waiting for `onResponse()` callback. Can you confirm that this is happening to you or to other people using that API too? And dont increase the timeout that much, it is useless. By that value you mean 50 minutes. – Talha Apr 10 '17 at 06:14
  • I am facing issue here, I do not know about the other people :) – Alok Apr 10 '17 at 06:15
  • Hi @Alok, Mobile can have 1minute timeout. Volley can internally implement retry if request fails for 3 times. I think there's a request time out and retry count. what is the longest response time you encountered when testing using REST client? – Sandeep Devhare Apr 10 '17 at 06:21
  • Maximum it takes approx 30 sec – Alok Apr 10 '17 at 06:24
  • 1
    @Talha 50000ms = 50s not 50m :) – Alok Apr 10 '17 at 07:19

1 Answers1

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, Please check with your Application class method addToRequestQueue() if there is already setup with retry policy and again you are setting up in above code. Also follow above methods to make request from application class. For more information please check on this thread too. Volley Timeout error

Community
  • 1
  • 1
Sandeep Devhare
  • 440
  • 1
  • 6
  • 16