android HttpClient 예제 get, post 방식
get방식
HttpClient client = new DefaultHttpClient(); String url = "http://localhost:8080"; HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity resEntity = response.getEntity(); if(resEntity != null){ Log.w("reponse", EntityUtils.toString(resEntity)); }
위 소스에서 try~ catch를 적용하면
HttpClient client = new DefaultHttpClient(); String url = "http://localhost:8080"; HttpGet get = new HttpGet(url); HttpResponse response = null; try { response = client.execute(get); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity(); String sRes = ""; if(resEntity != null){ try { sRes = EntityUtils.toString(resEntity); //sRes = URLDecoder.decode(sRes); Log.w("SNSApp", "response: " + sRes); Toast.makeText(mContext, sRes, Toast.LENGTH_SHORT).show(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
post방식
HttpClient client = new DefaultHttpClient(); String postUrl = "http://222.jsp"; HttpPost post = new HttpPost(postUrl); List params = new ArrayList(); params.add(new BasicNameVal!uePair("deliveryDate", "1111111111")); UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8); post.setEntity(ent); HttpResponse responsePost = client.execute(post); HttpEntity resEntity = responsePost.getEntity(); if(resEntity != null){ Log.w("Response", EntityUtils.toString(resEntity)); }
출처 : http://cafe.naver.com/infinityjava/206
'안드로이드 개발 팁' 카테고리의 다른 글
android 강제 키 이벤트 발생 (0) | 2012.06.01 |
---|---|
자바 날짜 시간 계산 예제 코드 모음 (0) | 2012.05.30 |
android twitter 연동 1 ( 앱등록및 라이브러리 다운로드) (1) | 2012.05.24 |
android facebook 연동 1 (앱등록 및 SDK 다운로드 및 실행) (3) | 2012.05.24 |
Android keyboard, keypad (0) | 2012.05.22 |