【发布时间】:2015-02-14 19:30:33
【问题描述】:
我正在从 asynctask 迁移到 volley。在我的应用程序中,我想要来自我的 WCF REST 服务的 json 数据。我的代码如下
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
if(error.networkResponse != null && error.networkResponse.data != null){
VolleyError error2 = new VolleyError(new String(error.networkResponse.data));
error = error;
}
Log.d("ERROR", "error => " + error.toString());
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params1 = new HashMap<>();
params1.put("userid", "123");
params1.put("password", "123");
return params1;
}
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
} ;
但我收到来自 WCF 服务的错误“HTTP 错误 400。请求的标头名称无效”...
【问题讨论】:
-
“请求的标头名称无效”我认为服务器不会期望
application/json也许它想要application/x-www-form-urlencoded或其他东西所以尽量不要发送任何标头。 -
使用 google chrome 网络分析器或 fiddler 查看 http 消息。这可以帮助您了解问题所在。
-
mmlooloo 先生,我都试过了,但仍然出现错误 400...
-
sir rafa,我在 fiddler 和 restclient 中看到过..都给了我我期望的 json