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