我想发一个新JsonObjectRequest请求:
我想将带有此请求的JSON格式数据发送到服务器
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST, "myurl.com", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//...
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//...
}
})
{
@Override
protected Map<String,String> getParams() {
// something to do here ??
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// something to do here ??
return params;
}
};
Run Code Online (Sandbox Code Playgroud)PS我也在我的项目中使用GSON库.
我正在使用android Volley提出请求.所以我使用这段代码.我不明白一件事.我检查我的服务器,params始终为null.我认为getParams()不起作用.我该怎么做才能解决这个问题.
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
hideProgressDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideProgressDialog();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("id","1");
params.put("name", "myname");
return params;
};
};
queue.add(jsObjRequest);
Run Code Online (Sandbox Code Playgroud) 我看到关于Volley的Google IO 2013会议,我正在考虑转向凌空.Volley是否支持添加POST/GET参数来请求?如果是,我该怎么办?
我正在尝试在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 …Run Code Online (Sandbox Code Playgroud)