'Volley requests get overlap
So i had this add to cart view : < 1 >. What i want is when press ">" the system will check if user already had a cart, if not, create one, then proceed to add the product to cart. I made two functions to do this: createCart() and addProductFirstTime(). But somehow they got "mixed" with each other, for example, i have a Log.d("Create cart") in createCart() and Log.d("Add product first time") in addProductFirstTime(). And when i ran it, the "create cart" in the place where "add product first time" should be . To summarize, some proceed should be performed in createCart() appear in addProductFirstTime() and vice versa. Can someone please tell me what is going on? I very appreciate it.
This is how i called createCart() and addProductFirstTime():
if(user.getIsAddFirstTime()==0){
user.setIsAddFirstTime(1);
createCart();
}
addProductFirstTime();
My function addProductFirstTime():
public void addProductFirstTime(){
Log.d("Info", "add first time");
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ADD_FIRST_TIME, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Info:", "Still in add first time");
try {
// Log.d("addfirsttimerespond is:", response + "Hello");
Log.d("addfirsttimecartid: ", Integer.toString(SharePrefManager.getInstance(mcontext).getCartid()));
JSONObject jsonObject = new JSONObject(response);
// Toast.makeText(mcontext, jsonObject.getString("message"), Toast.LENGTH_LONG).show();
// cart.setCartId(jsonObject.getInt("cartid"));
Log.d("addfirsttimecartid: ", Integer.toString(SharePrefManager.getInstance(mcontext).getCartid()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mcontext, error.getMessage(),Toast.LENGTH_LONG).show();
}
}){
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("productid", Integer.toString(productVariation.getId()));
params.put("price", Integer.toString(productVariation.getPrice()));
params.put("quantity", Integer.toString(productVariation.getQuantity()));
params.put("cartid", Integer.toString(SharePrefManager.getInstance(mcontext).getCartid()));
Log.d("Info","add product first time again");
//Log.d
return params;
}
};
requestHandler.getInstance(mcontext).addToRequestQueue(stringRequest);
// Volley.newRequestQueue(mcontext).add(stringRequest);
// requestQueue.add(stringRequest);
Log.d("mycartid: ",Integer.toString(SharePrefManager.getInstance(mcontext).getCartid()) );
}
My function createCart():
public void createCart(){
n++;
Log.d("n=", Integer.toString(n));
Log.d("Info", "Create cart");
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_CREATE_CART, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d("Info:", "Still in create cart");
JSONObject jsonObject = new JSONObject(response);
SharePrefManager.getInstance(mcontext).setCartId(jsonObject.getInt("cartid"));
// cart.setCartId(jsonObject.getInt("cartid"));
Log.d("create cart id is:", Integer.toString( SharePrefManager.getInstance(mcontext).getCartid()));
// Toast.makeText(mcontext, jsonObject.getInt("cartid"), Toast.LENGTH_LONG).show();
// Log.d("cart id is:" , Integer.toString(jsonObject.getInt("cartid")));
// Log.d("cartid: ", Integer.toString(cart.getCartId()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mcontext, error.getMessage(),Toast.LENGTH_LONG).show();
}
}){
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("userid", Integer.toString(user.getId()));
Log.d("Info","Create cart again");
Log.d("Info", "trang thai tao gio hang: "+ Integer.toString(user.getIsAddFirstTime()));
return params;
}
};
// Volley.newRequestQueue(mcontext).add(stringRequest);
requestHandler.getInstance(mcontext).addToRequestQueue(stringRequest);
//requestQueue.add(stringRequest);
}
This is what i got in the console:
D/Info: Create cart
D/Info: add first time
D/mycartid:: 90
D/Info: Create cart again
D/Info: trang thai tao gio hang: 1
D/Info: add product first time again
D/Info:: Still in add first time
D/addfirsttimecartid:: 90
//// a bunch of errors, they belong to addProductFirstTime() btw
D/Info:: Still in create cart
D/create cart id is:: 91
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
