我正在使用我的应用程序中的凌空库来处理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会话中保留修改后的值来完成的,但是当使用android app进行第二次服务调用时,我无法获得更新后的响应。响应仍然是第一个。我知道排球库无法通过引用此链接来维护cookie。 Volley Library for cookies
这是我用来解析API响应的GsonRequest类。
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库中维护该会话而必须进行的更改,以便能够获得更新的响应。任何帮助是极大的赞赏。预先感谢。
答案 0 :(得分:0)
Magento会话值存储为cookie。我无法获取这些值,因为默认情况下 volley库不接受并从服务器读取cookie 。我从答案Using cookies with Volley中找到了解决方案。请仔细阅读以便更好地理解。