【发布时间】:2019-01-18 18:22:36
【问题描述】:
我的代码正在尝试将数据发布到服务器,我需要添加一个标头,我正在使用 Volley 库。
如果我不包含“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<String>() {
@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<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}
////
@Override
public Map<String, String> getParams() {
Map<String, String> 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);
}
【问题讨论】:
标签: android post android-volley