반응형
java에서 http에 request를 요청할때 post방법과 get방법이 있는데..
그중에서 post방법으로 요청을 하게 될 때 필요한 방법입니다.
핵심은
conn.setDoOutput(true);
이렇게 해 주면 됩니다.get방식으로 하게 된다면
conn.setDoInput(true);
로 하게 되면 get방식이 됩니다.
try {
// Construct data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
// Send data
URL url = new URL("http://address/");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
}
wr.close();
rd.close();
} catch (Exception e) {
}
반응형
'Android' 카테고리의 다른 글
| [Android] Android에서 virtual(soft) keyboard 숨기고 보이게 하는 방법 (0) | 2010.01.25 |
|---|---|
| [Android] 화면 회전 방지 방법. (1) | 2010.01.23 |
| [Android] java.net.SocketException: Permission denied (maybe messing INTERNET permission) 에러 대처방법 (1) | 2010.01.23 |
| [Android] Emulator에서 .apk 파일 설치 방법 (4) | 2010.01.14 |
| [Android] Android SDK 2.0 설치하기. (1) | 2009.12.18 |