凌空JsonObjectRequest参数后不再工作 [英] Volley JsonObjectRequest Post parameters no longer work

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

问题描述

我试图在一个凌空JsonObjectRequest发送POST参数。最初, 这是工作我按照什么官方的code说做传递包含在JsonObjectRequest的构造函数的参数,一个JSONObject的。然后突然就停止工作,我还没有作出了code,这是previously工作的任何变化。服务器不再承认任何POST参数发送。这里是我的code:

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 code:

Here is the simple tester PHP code on the server:

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

我得到的回应是 {标签:空} 结果
昨天,它工作得很好,并与 {标签:测试}是在回应结果
我并没有改变单一的事情,但今天它已不再工作。

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.

在凌空源$ C ​​$ C构造的javadoc它说,你可以在构造函数传递一个JSONObject以@参数jsonRequest送岗位参数:
<一href=\"https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/JsonObjectRequest.java\" rel=\"nofollow\">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

/ **结果
  &NBSP; *创建一个新的请求结果。
  &NBSP; * @参数的方法来使用结果的HTTP方法
  &NBSP; * @参数网址URL从结果获取JSON
  &NBSP; * @参数jsonRequest A {@link的JSONObject}发布的请求。空是允许的,结果
  &NBSP; * NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;表示没有参数将与请求一起发布

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

<一个href=\"http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working\">Volley JsonObjectRequest发布采购信息不灵

<一个href=\"http://stackoverflow.com/questions/24570909/volley-post-jsonobjectrequest-ignoring-parameters-while-using-getheader-and-getp\">Volley帖子JsonObjectRequest忽略的参数,同时使用getHeader和getParams

<一个href=\"http://stackoverflow.com/questions/25945684/volley-not-sending-a-post-request-with-parameters\">Volley不发送一个带参数的post请求。

我试过设置的JSONObject在JsonObjectRequest构造为null,则覆盖,并在设置参数getParams()方法,getBody()和getPostParams()方法,但这些都不覆盖的一直为我工作。另一项建议是使用基本上是创建一个自定义的请求额外的辅助类,但解决办法是我的需要有点太复杂。如果它归结到它,我会想尽一切办法让它工作,但我希望有一个简单的道理,为什么我的code 工作,然后就 停止,还有一个简单的解决方案。

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.

推荐答案

最后我用乱射的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,但使用字符串来代替。

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

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

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