25
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VolleyさんでRequestをPOSTで送信して、ResponseをJSONで受け取る

Posted at

少しVolleyさんに手を出してみました。
ResponseのJSONをGsonで解析してくれるカスタムRequestを作成している人がいましたが、RequestのメソッドがGETだけだったので、ソースを拝借して、POSTで送信できるようにしてみました。パラメータはHashMapで設定します。

[参考]
GsonRequest.java
VolleyのRequestを拡張してPOST/GETのリクエストしてみる
Google I/O 2013 - Android : Volley: Easy, Fast Networking for Android

[ソース]
GsonRequest.java改

public class Sample {

	public void request() {}
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("param1", value1);
		params.put("param2", value2);

		GsonRequest<Result> request = new GsonRequest<Result>(Method.POST, "http://www.com", Result.class,
		params, successListener, errorListener);
		RequestQueue queue = Volley.newRequestQueue(context);
		queue.add(request);
	}

	private Listener<Result> successListener = new Listener<Result>() {

		@Override
		public void onResponse(Result response) {
		}
	}

	private Response.ErrorListener errorListener = new Response.ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError error) {
		}

	}
}

もう他にこーゆークラスがあるかもしれませんが、お勉強ということで。。。

25
25
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
25
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?