我正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,还有一个简单的解决方案.