我想在Volley JsonObjectRequest中发送POST参数。最初,它正在为我工作遵循官方代码所说的在JsonObjectRequest的构造函数中传递包含参数的JSONObject。然后突然它停止工作,我没有对之前的代码做任何改变。服务器不再识别任何POST参数正在发送。这里是我的代码:排气JsonObjectRequest发布参数不再工作
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"}
是在回应我没有改变一件事,但今天它不再有效。
处抽射源代码构造的javadoc它说,你可以在构造函数传递一个JSONObject以“@参数jsonRequest”送岗位参数: https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/toolbox/JsonObjectRequest.java
/**
*创建新的请求。
* @参数方法的HTTP方法使用
* @参数网址URL从
取JSON * @参数jsonRequest A {@link的JSONObject}发布该请求。空是允许的,
* 表示没有参数将与请求一起发布。
我已阅读其他职位有类似的问题,但解决方案并没有为我工作:
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()”,“get Body()“和”getPostParams()“方法,但这些覆盖都没有为我工作。另一个建议是使用一个基本上创建自定义请求的辅助类,但该修复对我的需求来说有些复杂。如果归结到它,我会尽一切努力使它工作,但我希望有一个简单的道理,为何我的代码为工作,然后就停止,也是一个简单的解。
您是否找到任何解决方案? – Dory 2015-07-23 08:46:22
@Dory是的,我找到了一个简单的解决方案。它没有解决JsonObjectRequest的问题,我完全放弃了JsonObjectRequest,因为解决方案是使用Volley的StringRequest。查看我的答案,看看我做了什么。 – 2015-07-24 13:54:14
检查此链接http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working。 – 2015-09-19 13:25:28