0

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 the params 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();
}
Kasnady
  • 2,249
  • 3
  • 25
  • 34
  • 1
    met this problem while using the Volley library. instead, I used the StringRequest and converted the String data to JSON array when received. still havent found an explanation for it. I'd advise to use StringRequest. If the server is echo-ing the json data back then you can convert in onResponse() – Manny265 Dec 31 '17 at 11:49
  • Hi @Manny265 i had tried to convert to StringRequest. But still unable – Kasnady Jan 01 '18 at 02:19

1 Answers1

0

I solved my own problem Sent POST request but server says GET request in Android volley, what I am doing wrong here?

Solution: Due to i'm requesting to PHP server, i need to point out the filename.

Currently i point the request to a URL without Filename extension, and i just add index.php to my end of URL and it worked.

Kasnady
  • 2,249
  • 3
  • 25
  • 34