Search code examples
androidjsonhttpandroid-volleyjsonobjectrequest

Volley JsonObjectRequest error response code 500


when i try to post json and take result as a json i get response code 500 error. But if i try this with string request there is no problem. I searched a lot on stackoverflow, there are other people like me having this problem but there is no up-to-date solution for this. So may anyone show me how i can do json object post request by up-to-date method(with also php side) please?

Android side:

    Map<String, String> map = new HashMap<String, String>(); 
    map.put("user", username);
    map.put("pass", password);

    JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, "http://www.example.com/example/.php?action=signin", new JSONObject(map), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject result) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();

            return params;
        }
    };

PHP side:

$post = json_decode(file_get_contents("php://input"), true);
        $user = $post['user'];
        $pass = $post['pass'];

$array = array("entrance" =>  $example);

echo json_encode($array,true);

Solution

  • I fixed the problem, there was only one element in my json object which is a jsonarray actually. After i changed jsonobjectrequest to jsonarrayrequest, the problem was solved.