Output: (URL: https://www.secureserver.net/api/v1/cart/508688?redirect=false)
Note: Please try with a privateLabelId as the following output is a result of an HTTP 400 request
{
"error": {
"statusCode": 400,
"name": "invalid-product",
"message": "Bad Request. Invalid {productId} in {items}"
}
}
Headers
com.example.test E/MainActivity: Name Access-Control-Allow-Credentials Value true
com.example.test E/MainActivity: Name Cache-Control Value max-age=0, no-cache, no-store
com.example.test E/MainActivity: Name Connection Value close
com.example.test E/MainActivity: Name Content-Length Value 109
com.example.test E/MainActivity: Name Content-Type Value application/json; charset=utf-8
com.example.test E/MainActivity: Name Date Value Thu, 21 Feb 2019 06:09:34 GMT
com.example.test E/MainActivity: Name Expires Value Thu, 21 Feb 2019 06:09:34 GMT
com.example.test E/MainActivity: Name P3P Value CP="IDC DSP COR LAW CUR ADM DEV TAI PSA PSD IVA IVD HIS OUR SAM PUB LEG UNI COM NAV STA"
com.example.test E/MainActivity: Name Pragma Value no-cache
com.example.test E/MainActivity: Name Server Value nginx
com.example.test E/MainActivity: Name Vary Value Origin, Accept-Encoding
com.example.test E/MainActivity: Name X-Android-Received-Millis Value 1550729374476
com.example.test E/MainActivity: Name X-Android-Response-Source Value NETWORK 400
com.example.test E/MainActivity: Name X-Android-Selected-Protocol Value http/1.1
com.example.test E/MainActivity: Name X-Android-Sent-Millis Value 1550729373659
com.example.test E/MainActivity: Name X-ARC Value 102
com.example.test E/MainActivity: Name X-Content-Type-Options Value nosniff
com.example.test E/MainActivity: Name X-Download-Options Value noopen
com.example.test E/MainActivity: Name X-Frame-Options Value DENY
com.example.test E/MainActivity: Name X-XSS-Protection Value 1; mode=block
class VolleyJsonRequest
import android.util.Log;
import android.support.annotation.Nullable;
import com.android.volley.Header;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
class VolleyJsonRequest {
private ResponseListener listener;
private int method;
private String url;
private List<Header> headers = new ArrayList<>();
private JSONObject body;
private int statusCode;
private static final String TAG = VolleyJsonRequest.class.getSimpleName();
public VolleyJsonRequest(int method, String url, JSONObject body, ResponseListener listener) {
this.listener = listener;
this.method = method;
this.url = url;
this.body = body;
}
public Request get() {
return new Request(method, url, body.toString(), responseListener, errorListener);
}
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
deliverResult(response);
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
statusCode = error.networkResponse.statusCode;
headers.addAll(error.networkResponse.allHeaders);
String json;
try {
json = new String(
error.networkResponse.data,
HttpHeaderParser.parseCharset(error.networkResponse.headers));
deliverResult(json);
} catch (
UnsupportedEncodingException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
};
private void deliverResult(String response) {
try {
Object object = new JSONTokener(response).nextValue();
if (object instanceof JSONObject) {
listener.onResponse(statusCode, headers, (JSONObject) object, null);
} else {
listener.onResponse(statusCode, headers, null, (JSONArray) object);
}
} catch (JSONException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
class Request extends JsonRequest {
public Request(int method, String url, @Nullable String requestBody, Response.Listener listener, @Nullable Response.ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
headers.addAll(response.allHeaders);
statusCode = response.statusCode;
String string;
try {
string = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
} catch (
UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
return Response.success(
string,
HttpHeaderParser.parseCacheHeaders(response));
}
}
public interface ResponseListener {
void onResponse(int statusCode, List<Header> headers, JSONObject object, JSONArray array);
}
}
Test:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.android.volley.Header;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class MainActivity extends AppCompatActivity implements VolleyJsonRequest.ResponseListener {
private static final String TAG = MainActivity.class.getSimpleName();
private RequestQueue queue;
String requestBody = "{\n" +
" \"items\": [\n" +
" {\n" +
" \"id\": \"string\",\n" +
" \"domain\": \"string\"\n" +
" }\n" +
" ],\n" +
" \"skipCrossSell\": true\n" +
"}";
String url = "https://www.secureserver.net/api/v1/cart/508688?redirect=false";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue = Volley.newRequestQueue(this);
testOne();
testTwo();
}
private void testOne(){
JsonRequest request = new JsonRequest(Request.Method.POST, url, requestBody, new Response.Listener() {
@Override
public void onResponse(Object response) {
Log.e(TAG,"Response " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
String response = new String(
error.networkResponse.data,
HttpHeaderParser.parseCharset(error.networkResponse.headers));
Log.e(TAG,error.networkResponse.allHeaders.toString());
Log.e(TAG,response);
} catch (UnsupportedEncodingException e) {
Log.e(TAG,e.getMessage());
}
}
}) {
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
List<Header> headers = response.allHeaders;
for(Header header: headers){
Log.e(TAG,"Name " + header.getName() + " Value " + header.getValue());
}
String json = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
new JSONObject(json),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException e) {
return Response.error(new ParseError(e));
}
}
};
queue.add(request);
}
private void testTwo(){
VolleyJsonRequest jsonRequest = null;
try {
jsonRequest = new VolleyJsonRequest(Request.Method.POST,url, new JSONObject(requestBody),this);
queue.add(jsonRequest.get());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onResponse(int statusCode, List<Header> headers, JSONObject object, JSONArray array) {
Log.e(TAG,"-------------------------------");
for(Header header: headers){
Log.e(TAG,"Name " + header.getName() + " Value " + header.getValue());
}
if (object != null){
// handle your json object
}else if (array != null){
// handle your json array
}
}
}
I tried two ways i.e. testOne and testTwo. both are working fine. testOne is pretty straight forward for testTwo i've created a
VolleyJsonRequest class. You don't need this class if you are using the testOne method. this class is created just for the comfort/ease to use a common class structure in your project. This class encapsulate actual request and provides a custom interface via which you get the response. it has two special variables i.e. object and array. one of these will be null in case of a valid json response. which variable will be null depends upon the response sent by Server.