我试图发送一个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:
如果您使用Google的官方排球库,在您编辑的请求中,'params'应该是'JSONObject'而不是'HashMap'。希望这可以帮助! – BNK
看看我的答案[这里](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
在我编辑的REQUEST中,参数已经是JsonObject并且还没有工作。我看看你的答案!谢谢 – dvdciri