0

I am using volley library in my app for handling Magento API. For one particular API, the response is getting changed based on the operations done in the app.

Here is the response before making any changes:

[
  {
    "grand_total": 0,
    "subtotal": 7144,
    "shipping_amount": 0,
    "items_qty": 1,
    "items": [
      {
        "item_id": "1654",
        "price": 7144,
        "qty": 1,
        "name": "STYLUS ITEM"
      }
    ],
    "deductions": {
      "jss": null,
      "coupon": [  ],
      "gift_cards": [ ]
    }
  }
]

After making changes,when I call the API for the second time in POSTMAN, the response gets updated to

[
  {
    "grand_total": 0,
    "subtotal": 7144,
    "shipping_amount": 0,
    "items_qty": 1,
    "items": [
      {
        "item_id": "1654",
        "price": 7144,
        "qty": 1,
        "name": "STYLUS ITEM"
      }
    ],
    "deductions": {
      "jss": {
        "method": "amount",
        "customer_id": 394,
        "schemes": [
          {
            "msno": 145,
            "group_code": "GLN",
            "company_code": "GWO",
            "amount": 3496,
            "scheme_id": 143
          }
        ]
      },
      "coupon": [],
      "gift_cards": [ ]
    }
  }
]

This is done by maintaining the modified values in a magento session.But when using android app to make service call for second time, I cannot get the updated response. The response is still the first one.I came to know that volley library cannot maintain cookies by refering to this link. Volley Library for cookies

This is the GsonRequest class which I use to parse API response.

public class GsonRequest<T> extends Request<T> {

    private static final String TAG = GsonRequest.class.getSimpleName();
    /**
     * Charset for request.
     */
    private static final String PROTOCOL_CHARSET = "utf-8";

    /**
     * Content type for request.
     */
    private static final String PROTOCOL_CONTENT_TYPE =
            String.format("application/json; charset=%s", PROTOCOL_CHARSET);

    private final Listener<T> mListener;

    private final String mRequestBody;

    private Gson mGson;
    private Class<T> mJavaClass;
    private Map<String, String> headers;

    public GsonRequest(int method, String url, Class<T> cls, Map<String, String> header, String requestBody, Listener<T> listener,
                       ErrorListener errorListener) {
        super(method, url, errorListener);
        mGson = new Gson();
        mJavaClass = cls;
        mListener = listener;
        headers = header;
        mRequestBody = requestBody;

    }

    @Override
    protected void deliverResponse(T response) {
        mListener.onResponse(response);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers;
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            int statusCode = response.statusCode;
            Log.v(TAG + " Status Code", String.valueOf(statusCode));
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            T parsedGSON = mGson.fromJson(jsonString, mJavaClass);
            return Response.success(parsedGSON,
                    HttpHeaderParser.parseCacheHeaders(response));

        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException je) {
            return Response.error(new ParseError(je));
        }
    }

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

    @Override
    public byte[] getBody() {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
            return null;
        }
    }

}

And I am making the service call as follows:

GsonRequest gsonRequest = new GsonRequest<AmountDetailsResp[]>(Request.Method.POST, builder.toString(),
                AmountDetailsResp[].class, hashHeader, new Gson().toJson(amountDetailsReq), new Response.Listener<AmountDetailsResp[]>() {
            @Override
            public void onResponse(AmountDetailsResp[] responseArray) {
                fragment.stopProgressDialog();
                if (responseArray != null) {
                    for (int i = 0; i < responseArray.length; i++) {
                        AmountDetailsResp response = responseArray[i];
                        setCartTotalDetails(response.items.size(), response.subtotal, response.shipping_amount, response.grand_total);

                        fragment.getShippingDetailValue(response.items.size(), response.subtotal,
                                response.shipping_amount, response.grand_total,
                                fragment.checkedTextView.isChecked(), shippingAddressBean, billingAddressBean,
                                methodCode, carrierCode, addressId);
                    }

                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        fragment.stopProgressDialog();
                        Utility.showVolleyError(fragment.getContext(), TAG, error);
                    }
                });
        AppController.getInstance().addToRequestQueue(gsonRequest);

I want to know the changes that I have to make to enable cookie storage and maintain that session in volley library, so that I am able to get updated response. Any help is greatly appreciated. Thanks in advance.

Aadhil Ahmed
  • 111
  • 15

1 Answers1

0

Magento session values are stored as cookies. I wasn't able to fetch those values because by default volley library doesn't accept and read cookies from the server. I found out the solution from this answer Using cookies with Volley. Please go through it for better understanding.

Aadhil Ahmed
  • 111
  • 15