I made a Custom JSONObjectRequest as below code.
Currently i made the Request.Method.POST
and i had confirmed it while passing to the super class here
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
. It was received as int 1
(POST), but when received on my server (PHP), the log was receiving as "REQUEST_METHOD":"GET"
Have anyone met this kind of problem or can help to point out what i had missed in below code. As you can see below, i had make sure below things:
params
not empty- there is function
getParams()
there and theparams
are not empty - i even put
getBody()
and make sure it not returning null
Refer below code for what i had tried to do
try {
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
if (jsonObject != null && jsonObject.length() > 0) {
try {
if (jsonObject.getInt("status") == ResponseCode.LOGIN_FAILED) {
SharedManager sharedManager = new SharedManager();
sharedManager.logoutUser(context, true);
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
callback.onSuccessResponse(jsonObject);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onErrorResponse(error);
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
try {
return JSONHelperConverter.jsonToMapString(params);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8;";
}
@Override
public byte[] getBody() {
try {
String postBody = null;
try {
postBody = createPostBody(JSONHelperConverter.toMapString(params));
postBody = params.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return postBody == null ? null : postBody.getBytes("utf-8");
} catch (NegativeArraySizeException n) {
n.printStackTrace();
return null;
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
return null;
}
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
String responseString;
JSONObject jsonObject = null;
if (response != null) {
try {
responseString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
jsonObject = new JSONObject(responseString);
} catch (Exception e) {
e.printStackTrace();
}
}
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
}
};
NetworkSingleton.getInstance(context).addToRequestQueue(stringRequest);
} catch (NegativeArraySizeException n) {
n.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}