LoginSignup
12
12

More than 5 years have passed since last update.

volleyで配列をPOSTしたいとき

Posted at

Androidデベロッパーの強い味方のVolleyで配列をPOSTする場合のサンプルです。

TestActivity
public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Request request = new Request<String>(Request.Method.POST, "URL", new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) {
                return Response.success(new String(networkResponse.data), getCacheEntry());
            }

            @Override
            protected void deliverResponse(String response) {

            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> postParams = new HashMap<String, String>();
                //POSTパラメーターに配列入れる
                postParams.put("hoge[0]","param1");
                postParams.put("hoge[1]","param2");
                return postParams;
            }

        };
        request.setRetryPolicy(new DefaultRetryPolicy(
                DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        request.setTag("TAG");

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(request);
    }
}
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12