2015-10-02 59 views
1

我试图发送一个JsonObjectRequest给我的服务器与一些PARAMS,但似乎像PARAMS不到达服务器。之前发布,所以我尝试各类建议在谷歌找到,但没有一个能正常工作..Android Volley JsonObjectRequest不发送参数

这是我JsonObjectRequest的代码:

RequestQueue queue = MySingleVolley.getInstance(ctx). 
      getRequestQueue(); 

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d("REQUEST_JSON_TO_SERVER", "Success: " + response.toString()); 
       } 
      },new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error); 
       } 
      }){ 
        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 
         HashMap<String, String> headers = new HashMap<String, String>(); 
         headers.put("Content-Type", "application/json"); 
         return headers; 
        } 
        @Override 
        protected Map<String, String> getParams() { 
         return params; 
        } 
      }; 

    MySingleVolley.getInstance(ctx).addToRequestQueue(jsObjRequest); 

这些都是我的PARAM及其他:

String url = "url"; 

    //create the hashMap of parameters 
    database_zappapp db = new database_zappapp(getApplicationContext()); 
    db.open(); 
    HashMap<String, String> params = new HashMap<>(); 
    params.put("action","myAction"); 
    params.put("nomeutente", db.getUsernameLogged()); 
    params.put("token", token); 
    db.close(); 


    //Send the request to the server 
    Global.RequestJsonToServer(getApplicationContext(), url, Request.Method.POST, params); 

在此先感谢您的帮助!

编辑2

我已经改变了我的PARAMS在此创建一个字符串jsonBody:

JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("action","gcmUserRegister"); 
     jsonObject.put("nomeutente",db.getUsernameLogged()); 
     jsonObject.put("token",token); 
    }catch(JSONException e){ 
     e.printStackTrace(); 
    } 
    String requestBody = jsonObject.toString(); 
    db.close(); 

和我的要求是这样与getBody():

JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d("REQUEST_JSON_TO_SERVER", "Success: " + response.toString()); 
       } 
      },new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error); 
       } 
      }){ 
        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 
         HashMap<String, String> headers = new HashMap<>(); 
         headers.put("Content-Type", "application/json"); 
         return headers; 
        } 
        @Override 
        public byte[] getBody() { 
         try { 
          return requestBody == null ? null : requestBody.getBytes("utf-8"); 
         } catch (UnsupportedEncodingException uee) { 
          VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 
            requestBody, "utf-8"); 
          return null; 
         } 
        } 
      }; 

但已经不工作了! =(

邮差屏幕:

找不到用户意味着它在进入if语句,所以它的工作原理。与Android的我收到 “结果”: “空”

邮递员屏幕,应用程序/ JSON:

enter image description here

+0

如果您使用Google的官方排球库,在您编辑的请求中,'params'应该是'JSONObject'而不是'HashMap'。希望这可以帮助! – BNK

+0

看看我的答案[这里](http://stackoverflow.com/questions/32745400/sending-a-post-request-with-jsonarray-using-volley/32775920#32775920)和[这里](http: //stackoverflow.com/questions/32197615/volley-send-jsonobject-to-server-with-post-method/32216762#32216762)和[here](http://stackoverflow.com/questions/32197615/volley-send -jsonobject到服务器与 - -方法交/ 32216762#32216762)。希望这可以帮助! – BNK

+0

在我编辑的REQUEST中,参数已经是JsonObject并且还没有工作。我看看你的答案!谢谢 – dvdciri

回答

1

更改代码的这一部分:

`JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null ...` 

要这样:

`JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,yourparams..` 

原因:如果您使用的是默认的构造函数凌空那送PARAMS到服务器的方式。

1

我找到了解决方案!

的问题是在服务器不是在客户端,我正在使用POST,但是从客户端我会发送一个JSON对象,所以我的新的PHP是数据:

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

//echo "Action: ".$action; 
//Registrazione del token 
if($data['action'] == "gcmUserRegister"){ 
...... 

由于人很多BKS !

+0

很高兴你的问题解决了:)这里是另一个截图https://drive.google.com/file/d/0B2HGUM4c0YwpeE9YemdsZTA4Qk0/view?usp=sharing。此外,我是BNK,而不是BKS(哈哈) – BNK

+0

对不起,非常感谢! – dvdciri

相关问题