如果我不包含“ getparams”方法,则该请求有效,我可以发布但没有数据.
如果我包含“ getparams”方法,则请求失败,并显示400(错误请求).
我一直无法找出错误所在.
public void tryPost() {
RequestQueue queue = Volley.newRequestQueue(this);
String serverUrl = "http://10.0.2.2:3000/tasks";
StringRequest stringRequest = new StringRequest(Request.Method.POST, serverUrl,
new Response.Listener() {
@Override
public void onResponse(String response) {
Log.d("TAG", "response = "+ response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG", "Error = "+ error);
}
})
{
//
@Override
public Map getHeaders() {
HashMap headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}
////
@Override
public Map getParams() {
Map params = new HashMap<>();
params.put("userId","sargent");
params.put("password","1234567");
return params; //return the parameters
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/daiyoubiaotihezhengwendeandroid-volley-postqingqiu.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:11月 12, 2019 at 10:07 上午