android - Volley JsonObjectRequest Post 参数不再起作用

标签 android post android-volley 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”处发送 post 参数: https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/JsonObjectRequest.java

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

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

最佳答案

您只需从参数的 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);

关于android - Volley JsonObjectRequest Post 参数不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442977/

相关文章:

android - 我在哪里可以获得适用于 Android 手机的开源 Radio 驱动程序和 RIL?

android - Google Play Install Referrer API 与 INSTALL_REFERRER 广播

api - 如何在 Requests 中指定和实现 Telegram API 方法?

android - 使用样式在主题中设置 Activity 背景?

java - Android JCIFS 建立 session 失败

php - 使用 PHP 读取 JSON POST

jquery - 将 JQuery 点击事件与 JQuery post 混合

java - 设置 NetworkImageView 与 Volley 禁用图像的持有者

java - Android Volley 请求 - 在请求中填充自定义类不起作用

android - 我的数据未存储在数据库中