正文中的 Android Volley POST 字符串 [英] Android Volley POST string in body
问题描述
我正在尝试使用 Volley 库与我的 RESTful API 进行通信.
I'm trying to use Volley library to communicate with my RESTful API.
当我要求不记名令牌时,我必须在正文中 POST 字符串.字符串应如下所示:grant_type=password&username=Alice&password=password123和标题:内容类型:application/x-www-form-urlencoded
I have to POST string in the body, when I'm asking for the bearer Token. String should look like this: grant_type=password&username=Alice&password=password123 And header: Content-Type: application/x-www-form-urlencoded
有关 WebApi 个人帐户的更多信息:http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
More info about WebApi Individual Accounts: http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
不幸的是,我不知道如何解决它..
Unfortunately I can't figure out how can I solve it..
我正在尝试这样的事情:
I'm trying something like this:
StringRequest req = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "User0");
params.put("password", "Password0");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};
我一直收到 400 个错误请求.我认为我实际上是在发送这样的请求:
I'm getting 400 Bad Request all the time. I think that I'm actually sending request like this:
grant_type:password, username:User0, password:Password0
代替:
grant_type=password&username=Alice&password=password123
如果有人有任何想法或建议,我将不胜感激..
I would be very grateful if anyone has any ideas or an advice..
推荐答案
首先,我建议您通过打印到日志或使用诸如wireshark 或 fiddler 之类的网络嗅探器来准确查看您发送的内容.
First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.
尝试将参数放入正文怎么样?如果您仍然需要 StringRequest
,则需要扩展它并覆盖 getBody()
方法(类似于 JsonObjectRequest
)
How about trying to put the params in the body? If you still want a StringRequest
you'll need to extend it and override the getBody()
method (similarly to JsonObjectRequest
)
这篇关于正文中的 Android Volley POST 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!