我正在应用程序中使用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会话中维护修改后的值来完成的。但是当第二次使用android应用程序进行服务调用时,我无法获得更新的响应。响应仍然是第一个。我知道Volley库不能通过引用这个链接来维护cookie。
Volley Library for cookies
这是我用来解析API响应的gsonrequest类。
public class GsonRequest<T> extends Request<T> {
private static final String TAG = GsonRequest.class.getSimpleName();
private static final String PROTOCOL_CHARSET = "utf-8";
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库中维护会话,我必须进行哪些更改,以便能够得到更新的响应。非常感谢您的帮助。事先谢谢。