体のAndroid Volley Post String
-
22-12-2019 - |
質問
私はRestful APIと通信するためにバレーライブラリを使いようとしています。
私がベアラトークンを求めるとき、体内に文字列を投稿する必要があります。文字列は次のようになります。 GRANT_TYPE=パスワードとusername= alice&password= password123 そしてヘッダー: content-type:application / x-www-form-urlencoded
詳細WebAPIの個々のアカウントに関する情報: > http://www.asp.net/web-api/overview/セキュリティ/個人 - アカウント-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
.
誰かがいかなるアイデアやアドバイスを持っていれば非常に感謝します。
解決
最初のこと、私はあなたがログに印刷しているか、またはWireSharkやFiddlerのようなネットワークスニファを使用して送信しているのかを正確に確認します。
身体にパラメータを置くことをどのようにしていますか?それでもStringRequest
が必要な場合は、それを拡張してgetBody()
メソッドをオーバーライドする必要があります(JsonObjectRequest
と同様に)
他のヒント
通常の投稿要求(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);
}
.
そしてボリティストリングのバンドのデータとして任意の文字列を送るために、あなたは 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);
}
.
脇には、Valleyのドキュメントは存在しないため、スタックオーバーフローの質はかなり悪いです。このような例での答えを信じることはできません。
これは古いことを知っていますが、私はこの同じ問題に遭遇し、ここで見つけたはるかにクリーンなソリューションIMOが見つかりました: