자바 표준으로 URL class등을 사용하여 네트워크 리소스를 사용할수도 있지만, 각종 설정들을 조절하고 더 사용하기 쉬운방법이 있는데, 그것이 바로 HttpClient Class를 사용하는 방법이다.
GET, POST 메소드등을 빈번하게 구분해서 사용하고, 각종 http 네트워크 리소스들을 사용자화 해서 개발할 필요가 있다면, URL class를 사용하는 것도 좋지만, HttpClient를 사용하는 방법도 알아두는 것도 권할만하다.
참고로, 내가 참여하고 있는 프로젝트는 HttpClient Class만을 사용하여 각종 데이터 싱크등을 구현하였다.
사용하는 법 자체는 쉽다.
package com.hopeisagoodthing.samplehttpclient; import java.io.IOException; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HTTP; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private static TextView textView = null; private static Handler handler = new Handler(){ public void handleMessage(final Message msg) { textView.setText(Integer.toString((Integer) msg.obj)); }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.result_text); Thread thread = new Thread(mRunnable); thread.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private Runnable mRunnable = new Runnable() { @Override public void run() { try { final String url = "http://coolkim.tistory.com"; final HttpGet get = new HttpGet(url); final HttpClient httpClient = getHttpClient(); final HttpResponse httpResponse = httpClient.execute(get); handleResponse(httpResponse); } catch (ClientProtocolException e) { Log.e(TAG, "ERROR : ClientProtocolException"); } catch (IOException e) { Log.e(TAG, "ERROR : IOException"); } } }; private HttpClient getHttpClient() { HttpClient httpClient = null; try { final HttpParams params = new BasicHttpParams(); // PARAMS HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(params, 1000 * 30); // 30s timeout. HttpConnectionParams.setSoTimeout(params, 1000 * 30); // 30s timeout. HttpConnectionParams.setSocketBufferSize(params, 512 * 1024); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpClientParams.setRedirecting(params, false); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); // SCHEME final SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); httpClient = new DefaultHttpClient(ccm, params); } catch (Exception e) { httpClient = new DefaultHttpClient(); } return httpClient; } private void handleResponse(final HttpResponse httpResponse) { final int status = httpResponse.getStatusLine().getStatusCode(); try { Message msg = handler.obtainMessage(); msg.obj = status; handler.sendMessage(msg); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
'코딩하고 > Android' 카테고리의 다른 글
Fragment 다루기 (아주 심플하게..) (0) | 2012.11.15 |
---|---|
아날로그 시계 만들기, 헐!!! 코딩없이 된다. (1) | 2012.11.14 |
File Observer 사용하기 (0) | 2012.10.21 |
ActionBar로 Tab 만들기 (19) | 2012.10.19 |
App에서 SystemProperties 가지고 오는 방법 (1) | 2012.10.18 |