私はhttpクライアントライブラリとしてVolleyを使用しています。 リクエストの一部としてペイロードの生データをVolleyで送信する必要がありますか? 投稿はHow to send Request payload to REST API in java?ペイロードをVolleyに追加する投稿のリクエスト
ですが、これはVolleyを使用してどのように達成できますか?
私はhttpクライアントライブラリとしてVolleyを使用しています。 リクエストの一部としてペイロードの生データをVolleyで送信する必要がありますか? 投稿はHow to send Request payload to REST API in java?ペイロードをVolleyに追加する投稿のリクエスト
ですが、これはVolleyを使用してどのように達成できますか?
djodjoとしてStringRequestを使用する必要があります。確かにここAndroid Volley POST string in body
@Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="your body as string";
// 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();
}
例:
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
チェックthe source and more info here
** UPDATE:**あなたのparams追加する必要がある場合は、単にgetParams()
例を上書きすることができます。
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "val1");
params.put("randomFieldFilledWithAwkwardCharacters","{{%stuffToBe Escaped/");
return params;
}
あなたは無効にする必要はありませんgetBody
あなたのelfは、Volleyがあなたのためにこれをやっているので、特別な文字をエンコードしません。
する必要はありません。ボレーはそれらを扱うことができます。私の更新された答えを確認してください。 – djodjo