Volley JsonObjectRequest Post参数不再有效

Jos*_* C. 32 post android android-volley jsonobject

我正在尝试在Volley JsonObjectRequest中发送POST参数.最初,通过遵循官方代码所说的传递包含JsonObjectRequest的构造函数中的参数的JSONObject来为我工作.然后它突然停止工作,我没有对以前工作的代码进行任何更改.服务器不再识别正在发送任何POST参数.这是我的代码:

RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://myserveraddress";

// POST parameters
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "test");

JSONObject jsonObj = new JSONObject(params);

// Request a json response from the provided URL
JsonObjectRequest jsonObjRequest = new JsonObjectRequest
        (Request.Method.POST, url, jsonObj, new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        });

// Add the request to the RequestQueue.
queue.add(jsonObjRequest);
Run Code Online (Sandbox Code Playgroud)

这是服务器上的简单测试程序PHP代码:

$response = array("tag" => $_POST["tag"]);
echo json_encode($response);
Run Code Online (Sandbox Code Playgroud)

我得到的回应是{"tag":null}
昨天,它工作正常并且回应{"tag":"test"}
我没有改变任何一件事,但今天它已经不再工作了.

在Volley源代码构造函数javadoc中,它表示您可以在构造函数中传递JSONObject以在"@param jsonRequest"发送post参数:https://android.googlesource.com/platform/frameworks/volley/+/master/src /main/java/com/android/volley/toolbox/JsonObjectRequest.java

/**
 *创建新请求.
 *@param方法HTTP方法使用
 *@param url URL从
 *@param jsonRequest A {@link JSONObject} 获取JSON 以发布请求.允许空值,
 *表示不会随请求一起发布参数.

我已阅读其他类似问题的帖子,但解决方案对我没有用处:

Volley JsonObjectRequest发布请求无效

使用getHeader和getParams时,Volley Post JsonObjectRequest忽略参数

Volley没有发送带参数的帖子请求.

我已经尝试将JsonObjectRequest构造函数中的JSONObject设置为null,然后覆盖并设置"getParams()","getBody()"和"getPostParams()"方法中的参数,但这些覆盖中没有一个适用于我.另一个建议是使用一个基本上创建自定义请求的辅助类,但该修复对我的需求来说有点过于复杂.如果它归结到它,我会尽一切努力使它工作,但我希望有一个简单的道理,为什么我的代码工作,然后就停止了,也是一个简单的解决方案.

Phi*_*ppe 43

您只需从参数的HashMap中创建一个JSONObject:

String url = "https://www.youraddress.com/";

Map<String, String> params = new HashMap();
params.put("first_param", 1);
params.put("second_param", 2);

JSONObject parameters = new JSONObject(params);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, parameters, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        //TODO: handle success
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        //TODO: handle failure
    }
});

Volley.newRequestQueue(this).add(jsonRequest);
Run Code Online (Sandbox Code Playgroud)

  • 正确.这正是我所做的.请查看我发布的示例代码.它起初工作,但突然停止工作.但是,我一直在使用StringRequest作为我的解决方案,并且从那时起就没有任何问题. (2认同)

Jos*_* C. 34

我最终使用了Volley的StringRequest,因为我在尝试使JsonObjectRequest工作时花费了太多宝贵的时间.

RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://myserveraddress";

StringRequest strRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response)
                    {
                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
                    }
                })
        {
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String> params = new HashMap<String, String>();
                params.put("tag", "test");
                return params;
            }
        };

queue.add(strRequest);
Run Code Online (Sandbox Code Playgroud)

这对我有用.它就像JsonObjectRequest一样简单,但使用的是String.


Gon*_*ado 13

我有类似的问题,但我发现问题不是在客户端,而是在服务器端.发送时JsonObject,需要像这样获取POST对象(在服务器端):

在PHP中:

$json = json_decode(file_get_contents('php://input'), true);
Run Code Online (Sandbox Code Playgroud)


ita*_*910 8

您可以使用StringRequest执行与JsonObjectRequest相同的操作,同时仍然可以轻松发送POST参数.你唯一要做的就是从你得到的请求String中创建一个JsonObject,然后从那里继续,就像它是JsonObjectRequest一样.

StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //Creating JsonObject from response String
                            JSONObject jsonObject= new JSONObject(response.toString());
                            //extracting json array from response string
                            JSONArray jsonArray = jsonObject.getJSONArray("arrname");
                            JSONObject jsonRow = jsonArray.getJSONObject(0);
                            //get value from jsonRow
                            String resultStr = jsonRow.getString("result");
                        } catch (JSONException e) {

                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters = new HashMap<String,String>();
                        parameters.put("parameter",param);

                        return parameters;
                    }

                };
                requestQueue.add(stringRequest);
Run Code Online (Sandbox Code Playgroud)