简体   繁体   English

在Android上使用Volley时服务器未收到JSON对象

[英]Server does not receive JSON object while using volley on android

I tried to use volley to send JSON object to server and get a response but it seems the server doesnt get the data. 我尝试使用Volley将JSON对象发送到服务器并获得响应,但是服务器似乎没有获取数据。

If i tried to just echo back it works, but if i try to read the JSON object on server and react, i get error on volley. 如果我只想回显它就可以了,但是如果我尝试读取服务器上的JSON对象并做出反应,我会在截击中出错。 I also try to echo back the JSON object and still get error. 我还尝试回显JSON对象,但仍然出现错误。 When i use HttpRequest everything works ok. 当我使用HttpRequest时,一切正常。 I added the same Headers as in HttpRequest and Volley, Maybe is connected to UTF8? 我添加了与HttpRequest和Volley中相同的Header,也许已连接到UTF8? which i do on setEntity 我在setEntity上做的

i get the data on PHP like this: 我得到这样的PHP数据:

$data = json_decode(file_get_contents('php://input'), true);

My volley code: 我的齐射代码:

JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST,
           URL, jsonObject ,
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {

                       Log.d(TAG,"Response: " + response.toString());

               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           })
            {     
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError { 
                        Map<String, String>  params = new HashMap<String, String>();  
                        params.put("Accept", "application/json");  
                        params.put("Content-Type", "application/json");

                        return params;  
                }
            };
    ;

Anyone have an idea? 有人有主意吗?

It turned out that my volley code was ok. 原来我的齐射代码还可以。 the problem was on the server side. 问题出在服务器端。

Using Breadbin comment and replacing the VolleyLog.e with error.printStackTrace() helped to find the problem. 使用Breadbin注释并将VolleyLog.e替换为error.printStackTrace()有助于发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM