Volley JsonObjectRequest Post 参数不再起作用 [英] Volley JsonObjectRequest Post parameters no longer work

查看:35
本文介绍了Volley JsonObjectRequest Post 参数不再起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send POST parameters in a Volley JsonObjectRequest. Initially, it was working for me by following what the official code says to do of passing a JSONObject containing the parameters in the constructor of the JsonObjectRequest. Then all of a sudden it stopped working and I haven't made any changes to the code that was previously working. The server no longer recognizes that any POST parameters are being sent. Here is my code:

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 代码:

Here is the simple tester PHP code on the server:

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

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

The response I get is {"tag":null}
Yesterday, it worked fine and was responding with {"tag":"test"}
I haven't changed a single thing, but today it is no longer working.

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

In the Volley source code constructor javadoc it says that you can pass a JSONObject in the constructor to send post parameters at "@param jsonRequest": https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/JsonObjectRequest.java

/**
 * 创建一个新请求.
 * @param method 要使用的 HTTP 方法
 * @param url URL 用于从
获取 JSON * @param jsonRequest 与请求一起发布的 {@link JSONObject}.允许为空并且
 *     表示不会随请求一起发布参数.

/**
 * 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.

我读过其他有类似问题的帖子,但这些解决方案对我不起作用:

I have read other posts with similar questions, but the solutions haven't worked for me:

Volley JsonObjectRequest 发布请求无效

Volley Post JsonObjectRequest 在使用 getHeader 时忽略参数和获取参数

Volley 不发送带有参数的 post 请求.

我尝试将 JsonObjectRequest 构造函数中的 JSONObject 设置为 null,然后覆盖和设置getParams()"、getBody()"和getPostParams()"方法中的参数,但没有覆盖这些方法对我有用.另一个建议是使用一个额外的帮助类,它基本上创建了一个自定义请求,但这个修复对于我的需要来说有点太复杂了.如果归结为它,我会做任何事情来使它工作,但我希望有一个简单的原因来解释为什么我的代码 工作,然后只是停止,也是一个简单的解决方案.

I've tried setting the JSONObject in the JsonObjectRequest constructor to null, then overriding and setting the parameters in the "getParams()", "getBody()", and "getPostParams()" methods, but none of those overrides has worked for me. Another suggestion was to use an additional helper class that basically creates a custom request, but that fix is a bit too complex for my needs. If it comes down to it I will do anything to make it work, but I am hoping that there is a simple reason as to why my code was working, and then just stopped, and also a simple solution.

推荐答案

我最终使用了 Volley 的 StringRequest,因为我花了太多宝贵的时间来让 JsonObjectRequest 工作.

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

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

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

这篇关于Volley JsonObjectRequest Post 参数不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆