-
안드로이드에서 POST 요청 보내기dev/android 2018. 12. 21. 16:24
# HttpURLConnection 과 BufferedWriter 를 이용하여 url 로 요청 전송
https://stackoverflow.com/questions/2938502/sending-post-data-in-android
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
out.close();
urlConnection.connect();# url 에 전달할 파라메터 추가하기
https://stackoverflow.com/a/29053050/3543411
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("firstParam", paramValue1)
.appendQueryParameter("secondParam", paramValue2)
.appendQueryParameter("thirdParam", paramValue3);
String query = builder.build().getEncodedQuery();Url.Builder 를 이용하여 간결한 코드 작성 가능.