[英]Android Volley POST string in body
我正在嘗試使用Volley庫與我的RESTful API通信。
當我要求載體令牌時,我必須在正文中發布字符串。 字符串應如下所示:grant_type = password&username = Alice&password = password123和標頭:Content-Type:application / x-www-form-urlencoded
有關WebApi個人帳戶的更多信息: http : //www.asp.net/web-api/overview/security/individual-accounts-in-web-api
不幸的是,我不知道該如何解決。
我正在嘗試這樣的事情:
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錯誤請求。 我認為我實際上是在發送這樣的請求:
grant_type:password, username:User0, password:Password0
代替:
grant_type=password&username=Alice&password=password123
如果有人有任何想法或建議,我將不勝感激。
要發送帶有用戶名和密碼等參數的常規POST請求(無JSON),通常需要覆蓋getParams()並傳遞參數映射:
public void HttpPOSTRequestWithParameters() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
// volley will escape this for you
params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
params.put("username", "Alice");
params.put("password", "password123");
return params;
}
};
queue.add(postRequest);
}
並在Volley StringRequest中發送任意字符串作為POST正文數據,請重寫getBody()
public void HttpPOSTRequestWithArbitaryStringBody() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
@Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="grant_type=password&username=Alice&password=password123";
// usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it
try {
httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
} catch (UnsupportedEncodingException exception) {
Log.e("ERROR", "exception", exception);
// return null and don't pass any POST string if you encounter encoding error
return null;
}
return httpPostBody.getBytes();
}
};
queue.add(postRequest);
}
順便說一句,Volley文檔不存在,並且StackOverflow答案的質量非常差。 無法相信這樣的例子的答案還沒有在這里。
首先,我建議您通過打印到日志或使用諸如Wireshark或Fiddler之類的網絡嗅探器來准確查看要發送的內容。
試圖將參數放入體內怎么樣? 如果仍然需要StringRequest
,則需要對其進行擴展並覆蓋getBody()
方法(類似於JsonObjectRequest
)
我知道這很舊,但是我遇到了同樣的問題,並且在這里找到了一個更干凈的解決方案imo: 如何使用帶字符串主體的凌空來發送POST請求?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.