【问题标题】:Volley JsonObjectRequest error response code 500Volley JsonObjectRequest 错误响应代码 500
【发布时间】:2015-10-07 22:09:33
【问题描述】:

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

安卓端:

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

【问题讨论】:

  • 500是服务器端错误你的php代码有错误
  • 错误在哪里?我也放了php代码。一切看起来都很好。 :(
  • $array = array("入口" => $example); $exaple 在哪里?你发布完整的代码??
  • 这是另一个变量。我没有发布完整的 php 代码。我认为问题不在于其余部分。
  • 读取我所知道的apache中的php错误日志

标签: android json http android-volley jsonobjectrequest


【解决方案1】:

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

【讨论】:

  • 怎么只有一个元素?我可以在这里看到登录名和密码 - 这是两个元素,不是吗?
【解决方案2】:

试试这个

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

我认为你的 json 有问题..这就是为什么在服务器上 一边它抛出错误和服务器错误 500 来了

【讨论】:

  • 检查您的网络服务在其参数中需要什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多