반응형
출처 - http://adsgear.tistory.com/49
HTTP GET
HTTP POST with File attachment
HTTP GET
try { HttpClient client = new DefaultHttpClient(); String getURL = "http://www.google.com"; HttpGet get = new HttpGet(getURL); HttpResponse responseGet = client.execute(get); HttpEntity resEntityGet = responseGet.getEntity(); if (resEntityGet != null) { //do something with the response Log.w("RESPONSE",EntityUtils.toString(resEntityGet)); } } catch (Exception e) { e.printStackTrace(); }HTTP POST
try { HttpClient client = new DefaultHttpClient(); String postURL = "http://somepostaddress.com"; HttpPost post = new HttpPost(postURL); Listparams = new ArrayList (); params.add(new BasicNameValuePair("user", "kris")); params.add(new BasicNameValuePair("pass", "xyz")); 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)); } } catch (Exception e) { e.printStackTrace(); }
HTTP POST with File attachment
File file = new File("path/to/your/file.txt"); try { HttpClient client = new DefaultHttpClient(); String postURL = "http://someposturl.com"; HttpPost post = new HttpPost(postURL); FileBody bin = new FileBody(file); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("myFile", bin); post.setEntity(reqEntity); HttpResponse response = client.execute(post); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { Log.w("RESPONSE",EntityUtils.toString(resEntity)); } } catch (Exception e) { e.printStackTrace(); }
반응형
'개발 > 안드로이드' 카테고리의 다른 글
POST방식으로 파일과 텍스트 함께 전송 multipart/form-data이용 (0) | 2011.08.24 |
---|---|
안드로이드(android)에서 java 의 HttpClient 4.0 클래스를 이용한 네트웍 프로그램 구현 (0) | 2011.08.24 |
안드로이드 페이스북(facebook) 연동 #3 (0) | 2011.08.22 |
안드로이드 페이스북(facebook) 연동 #2 (0) | 2011.08.22 |
안드로이드 페이스북(facebook) 연동 #1 (0) | 2011.08.22 |
댓글