Volley JsonObjectRequest error response code 500

廉价感情. 提交于 2019-12-25 03:52:06

问题


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);

回答1:


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.




回答2:


try this

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



来源:https://stackoverflow.com/questions/33003260/volley-jsonobjectrequest-error-response-code-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!