volley post get 방법
출저 : http://stackoverflow.com/questions/16626032/volley-post-get-parameters
For the GET parameters there are two alternatives:
First: As suggested in a comment bellow the question you can just use String and replace the parameters placeholders with their values like:
String uri = String.format("http://somesite.com/some_endpoint.php?param1=%1$s¶m2=%2$s",
num1,
num2);
StringRequest myReq = new StringRequest(Method.GET,
uri,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
where num1 and num2 are String variables that contain your values.
Second: If you are using newer external HttpClient (4.2.x for example) you can use URIBuilder to build your Uri. Advantage is that if your uri string already has parameters in it it will be easier to pass it to the URIBuilder
and then use ub.setQuery(URLEncodedUtils.format(getGetParams(), "UTF-8"));
to add your additional parameters. That way you will not bother to check if "?" is already added to the uri or to miss some & thus eliminating a source for potential errors.
For the POST parameters probably sometimes will be easier than the accepted answer to do it like:
StringRequest myReq = new StringRequest(Method.POST,
"http://somesite.com/some_endpoint.php",
createMyReqSuccessListener(),
createMyReqErrorListener()) {
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("param1", num1);
params.put("param2", num2);
return params;
};
};
queue.add(myReq);
e.g. to just override the getParams()
method.
You can find a working example (along with many other basic Volley examples) in the Andorid Volley Examples project.
출저 : http://stackoverflow.com/questions/16626032/volley-post-get-parameters
'Android > 개발팁' 카테고리의 다른 글
json string 을 object 로 변환 (0) | 2015.10.23 |
---|---|
android spinner onitemselected 최초에 실행되는 문제 (0) | 2014.12.03 |
[Android] 핸드폰 고유값을 서버에 전송할 수 있을까? (0) | 2014.11.27 |
Android 에뮬레이터 웹브라이저에서 localhost 입력 (0) | 2013.12.16 |
태블릿 분기하기 (0) | 2013.10.08 |