Advertisement
Nesswit

Android HTTP Post Request

Apr 19th, 2013
491
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * HTTP POST 전송
  3.  * @param url 목적 URL
  4.  * @param params 함께 보낼 파라미터
  5.  *
  6.  * @return Response String
  7.  */
  8. private String httpRequestPost(final String url, final ArrayList<BasicNameValuePair> params) {
  9.     //타임아웃 설정
  10.     final int timeoutSeconds = 15;
  11.    
  12.     //Callable 선언
  13.     final Callable<String> postCallable = new Callable<String>() {
  14.         @Override
  15.         public String call() throws Exception {
  16.             final HttpClient client = new DefaultHttpClient();
  17.             final UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
  18.             final HttpResponse responsePost = client.execute(new HttpPost(url).post.setEntity(ent));
  19.             final HttpEntity resEntity = responsePost.getEntity();
  20.             return EntityUtils.toString(resEntity);
  21.         }
  22.     };
  23.  
  24.     try {
  25.         //callable 시작
  26.         final Future<String> postFuture = es.submit(postCallable);
  27.  
  28.         //결과값 반환
  29.         return postFuture.get(timeoutSeconds, TimeUnit.SECONDS);
  30.     } catch (Exception e) {}
  31.     return null;
  32. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement