Android的排球给我400错误 [英] Android Volley gives me 400 error
本文介绍了Android的排球给我400错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一个 POST
要求我的API,它工作在邮差
(我得到一个有效的JSON对象),但没有使用排球
。用下面的code:
I'm trying to make a POST
request to my API and it works in Postman
(I get a valid JSON object), but not using Volley
. With the following code:
String URL = "http://somename/token";
RequestQueue queue = Volley.newRequestQueue(StartActivity.this);
queue.add(new JsonObjectRequest(Method.POST, URL, null,
new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// handle response
Log.i("StartActivity", response.toString());
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle error
Log.i("StartActivity", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("username", "someUsername");
headers.put("password", "somePassword");
headers.put("Authorization", "Basic someCodeHere");
return headers;
}
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("grant_type", "client_credentials");
return params;
}
});
我收到以下错误:
I get the following error:
02-12 21:42:54.774: E/Volley(19215): [46574] BasicNetwork.performRequest: Unexpected response code 400 for http://somename/token/
我已经看到了很多的例子,我实在不明白是怎么回事错在这里。任何人任何想法?
I have seen a lot of examples and I don't really see what is going wrong here. Anyone any idea?
我更新了code用此方法:
HashMap<String, String> createBasicAuthHeader(String username, String password) {
HashMap<String, String> headerMap = new HashMap<String, String>();
String credentials = username + ":" + password;
String base64EncodedCredentials =
Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headerMap.put("Authorization", "Basic " + base64EncodedCredentials);
return headerMap;
}
和改变 getHeaders()
来:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return createBasicAuthHeader("username", "password");
}
仍然得到同样的错误!
Still getting the same error!
推荐答案
400指示错误的请求,也许你错过内容类型=应用程序/ JSON
您标题
400 indicates a bad request, maybe you're missing Content-Type=application/json
on your headers
这篇关于Android的排球给我400错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文