我正try 在截击JsonObjectRequest中发送POST参数.最初,按照官方代码所说的在JsonObjectRequest的构造函数中传递一个包含参数的JSONObject,对我来说是it was working.然后,它突然停止工作,我没有对以前工作的代码进行任何更改.服务器不再识别正在发送的任何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);

Here is the simple tester PHP code on the server:

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

我得到的回答是{"tag":null}
昨天,它运行正常,响应为{"tag":"test"}
我没有改变任何东西,但是今天它不再起作用了.

在Volley源代码构造函数javadoc中,它表示可以在构造函数中传递JSONObject,以便在"@param jsonRequest"处发送post参数:

/**
 * Creates a new request.
 * @param method the HTTP method to use
 * @param url URL to fetch the JSON from
 * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
 *       indicates no parameters will be posted along with request.

我看过其他有类似问题的帖子,但这些解决方案对我来说都不管用:

Volley JsonObjectRequest Post request not working

Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParams

Volley not sending a post request with parameters.

我曾try 将JsonObjectRequest构造函数中的JSONObject设置为null,然后覆盖并设置"getParams()"、"getBody()"和"getPostParams()"方法中的参数,但这些覆盖对我都不起作用.另一个建议是使用基本上创建自定义请求的附加助手类,但是该修复对于我的需要来说有点太复杂了.如果需要的话,我会做任何事情让它工作,但我希望有一个简单的原因,为什么我的代码was可以工作,然后只有stopped,还有一个简单的解决方案.

推荐答案

I ended up using Volley's StringRequest instead, because I was using too much valuable time trying to make JsonObjectRequest work.

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

This worked for me. Its just as simple as JsonObjectRequest, but uses a String instead.

Json相关问答推荐

合并二维数组的Jolt表达式

如何在对象投影(*)上应用滤镜投影([?port==`eth1`])?

使用更高级别架构中的字段值在$def内实现约束

来自json的可分析的构建报告

在Jolt中将具有嵌套元素的对象数组转换为单独的对象列表

遍历 JSON,检索父值

将来自 Golang 的 JSON API 调用响应输出到 nextjs 前端

修改 boost::json::object 中的值

MarkLogic REST 资源 API - 仅使用一个 POST 请求修补多个文档

Servicestack 返回数组而不是带有数组的对象

缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter

IE中Json响应下载(7~10)

如何通过 NSJSONSerialization 在 JSON 中包含空值?

Jsonpath 与 Jackson 或 Gson

关于使用 $ref 的 JSON 模式

如何向从文件中检索的 JSON 数据添加键值?

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

可以在 SharedPreferences 中保存 JSON 数组吗?

如何从 JSON 响应中提取单个值?

将 JsonArray 添加到 JsonObject