Volley Library 未在 Android 应用程序中获取 Magento 会话值

Volley Library not getting Magento session values in Android App

我在我的应用程序中使用 volley 库来处理 Magento API。对于一个特定的 API,响应会根据在应用程序中完成的操作而改变。

以下是进行任何更改之前的回复:

[
  {
    "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": [ ]
    }
  }
]

进行更改后,当我在 POSTMAN 中第二次调用 API 时,响应更新为

[
  {
    "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": [ ]
    }
  }
]

这是通过在 magento session.But 中维护修改后的值来完成的,当使用 android 应用程序第二次进行服务调用时,我无法获得更新的响应。回复还是第一个one.I参考这个link知道volley库不能维护cookies。 Volley Library for cookies

这是我用来解析 API 响应的 GsonRequest class。

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

}

我正在按如下方式进行服务调用:

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

我想知道我必须进行哪些更改才能启用 cookie 存储并在 volley 库中维护该会话,以便我能够获得更新的响应。任何帮助是极大的赞赏。提前致谢。

Magento 会话值存储为 cookie。我无法获取这些值,因为默认情况下 volley 库不接受和读取来自服务器 的 cookie。我从这个答案 Using cookies with Volley 中找到了解决方案。请仔细阅读以便更好地理解。