Nesswit

Android HTTP GET Request

Feb 14th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. /* @author Nesswit, Heewon Lee
  2.  * 이 소스 코드는 현재 사용중인 다른 프로젝트의 일부분을 가져온 것입니다.
  3.  * 따라서 이 코드만으로는 완전히 동작하지 않습니다.
  4.  */
  5.  
  6. /**
  7.  * HTTP GET 전송
  8.  * @param url 목적 URL
  9.  * @param params 함께 보낼 파라미터
  10.  *
  11.  * @return Response String
  12.  */
  13. public String httpRequestGet(final String url, final ArrayList<BasicNameValuePair> params) {
  14.     //반환할 Response String
  15.     String result = null;
  16.  
  17.     /*
  18.      * Callable을 통해 비동기적으로 처리
  19.      */
  20.     //Callable 선언
  21.     Callable<String> getCallable = new Callable<String>() {
  22.         @Override
  23.         public String call() throws Exception {
  24.             String ent = URLEncodedUtils.format(params, HTTP.UTF_8);
  25.             HttpGet get = new HttpGet(url + "?" + ent);
  26.             HttpResponse response = null;
  27.  
  28.             try {
  29.                 response = getClient().execute(get);
  30.             } catch (Exception e) {}
  31.  
  32.             HttpEntity resEntity = response.getEntity();
  33.             String res = null;
  34.  
  35.             try {
  36.                 res = EntityUtils.toString(resEntity);
  37.             } catch (Exception e) {}
  38.  
  39.             return res;
  40.         }
  41.     };
  42.  
  43.     //Callable 시작
  44.     Future<String> postFuture = es.submit(getCallable);
  45.  
  46.     try {
  47.         //결과값 반환
  48.         //R.integer.http_timeout_second 만큼 기다림.
  49.         result = postFuture.get(GlobalConstants.getHttpTimeoutSeconds(), TimeUnit.SECONDS);
  50.     } catch (Exception e) {}
  51.  
  52.     return result;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment