小编典典

Volley JsonObjectRequest Post参数不再起作用

json

我正在尝试在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);

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

$response = array("tag" => $_POST["tag"]);
echo json_encode($response);

我得到的答复是{"tag":null}
昨天,它运行良好,并且正在响应,但{"tag":"test"}
我没有改变任何事情,但是今天它不再起作用。

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

/
创建一个新请求。
* @param方法HTTP方法,用于使用
* @param URL URL从
* @param jsonRequest 获取JSON,并在其中发布一个{@link JSONObject}。允许为Null,并且
表示不随请求一起发布任何参数。

我尝试将JsonObjectRequest构造函数中的JSONObject设置为null,然后覆盖并设置“ getParams()”,“
getBody()”和“getPostParams()”方法中的参数,但这些覆盖均不适用于我。另一个建议是使用一个附加的帮助程序类,该类基本上创建一个自定义请求,但是对于我的需求而言,此修复太复杂了。如果它归结到它,我会尽一切努力使它工作,但我希望有一个简单的道理,为什么我的代码
工作,然后就 停止了 ,也是一个简单的解决方案。


阅读 422

收藏
2020-07-27

共1个答案

小编典典

我最终改用了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);

这对我有用。它与JsonObjectRequest一样简单,但是使用String代替。

2020-07-27