droid的凌空JsonObjectRequest不发送PARA

droid的凌空JsonObjectRequest不发送PARA

本文介绍了Android的凌空JsonObjectRequest不发送PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个JsonObjectRequest发送到我的一些PARAMS服务器,但好像PARAMS在服务器上不到位。之前发布,所以我尝试各种建议在谷歌找到,但没有一个人能正常工作。

I'm trying to send a JsonObjectRequest to my server with some params, but seems like params doesn't arrive at the server. Before to post on SO I try all kind of suggestion found in google but no one works fine..

这是我的JsonObjectRequest的code:

This is the code of my 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);

而这些是我的参数及其他:

And these are my param and others:

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

在此先感谢您的帮助!

Thanks in advance for the help!

编辑2

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

I've changed my params in this creating a string 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():

and my request like this with 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;
                        }
                    }
            };

但已经没有工作! =(

But already didn't work! =(

邮差屏幕:

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

No user found means that it enter in the if statement and so it works.. with android i receive "result": "null"

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

The postman screen with app/json:

推荐答案

我已经找到了解决办法!

I've found the solution!

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

The problem was in the server not in the client, I was getting the data using POST but from the client I was sending a json object so my new php is:

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

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

由于人很多BKS!

Thanks al lot to BKS!!!

这篇关于Android的凌空JsonObjectRequest不发送PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:47