[안드로이드] HttpPost 사용하기
안녕하세요 푸민입니다.
안드로이드 네트워크에서 HttpPost의 사용법을 볼까요?
일반적으로 HttpGet 같은 경우
HttpGet get = new HttpGet(url);
생성 후
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
각종 파라미터 설정
HttpResponse response = httpClient.execute(get);
이렇게 사용하는데요.
Post 같은 경우는
HttpPost post = new HttpPost(url);
넘길 파라미터값을
List<NameValuePair> params = new ArrayList<NameValuePair>();// 넘길파라미터
params.add(new BasicNameValuePair("version", 1 + ""));
params.add(new BasicNameValuePair("format", "json"));
params.add(new BasicNameValuePair("city", "서울"));
UrlEncodedFormEntity ent = null;// 엔티티정의
try {
ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);// 엔티티생성
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
post.setEntity(ent);// 포스트에 엔티티 붙이기
이런 식으로 추가를 해준뒤에
Get과 같은 방식으로 연결해주면 됩니다.
간단해요~