Volley JsonObjectRequest错误响应码500

Volley JsonObjectRequest error response code 500

当我尝试 post json 并将结果作为 json 时,我收到响应代码 500 错误。但是如果我用字符串请求尝试这个没有问题。我在 Whosebug 上搜索了很多,有像我这样的其他人也有这个问题,但没有最新的解决方案。所以有人可以告诉我如何通过最新方法(还有 php 方面)来做 json 对象 post 请求吗?

Android 边:

    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 边:

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

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

echo json_encode($array,true);

我解决了这个问题,我的 json 对象中只有一个元素实际上是一个 json 数组。我把jsonobjectrequest改成jsonarrayrequest后问题就解决了

试试这个

JSONObject map = new JSONObject(); 
map.put("user", username);
map.put("pass", password);

JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, "http://www.example.com/example/.php?action=signin", map, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject result) {
        Log.e("ServiceCall", result.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("ServiceCall", error.toString());
    }
});
RequestQueue mRequestQueue = Volley.newRequestQueue(<Activity>.this);
mRequestQueue.add(sr);

i think you have problem with your json ..and thats why at the server side it throws error and server error 500 comes