Volley JsonObjectRequest错误响应代码500 [英] Volley JsonObjectRequest error response code 500
问题描述
当我尝试发布json并将结果作为json时,我得到响应代码500错误。但是,如果我用字符串请求尝试这个没有问题。我在stackoverflow上搜索了很多,还有其他像我这样的人有这个问题,但没有最新的解决方案。所以有人可以告诉我如何通过最新的方法(还有php方面)做json对象发布请求吗?
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方面:
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方:
$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对象实际上是一个jsonarray。在我将jsonobjectrequest更改为jsonarrayrequest之后,问题就解决了。
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.
这篇关于Volley JsonObjectRequest错误响应代码500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!