1. 안드로이드의 HttpClient 을 이용하여 웹서버로 데이터를 POST 방식으로 전달하였다.
public int sendHttp() {
// URL address insert.
String url = "http://192.168.0.6:8080/webprog_lib/CheckLogin.jsp";
// HttpClient Create.
HttpClient client = new DefaultHttpClient();
// NameValuePair Type component create.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", text_id.getText()
.toString()));
nameValuePairs.add(new BasicNameValuePair("password", text_pwd
.getText().toString()));
// response over 7second timeout.
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 7000);
HttpConnectionParams.setSoTimeout(params, 7000);
// post send
HttpPost httpPost = new HttpPost(url);
// encoding.
UrlEncodedFormEntity entityRequest = new UrlEncodedFormEntity(nameValuePairs, "euc-kr");
httpPost.setEntity(entityRequest);
// The data is sending and waiting response.
HttpResponse response = client.execute(httpPost);
// response data type 200 normal.
if (response.getStatusLine().getStatusCode() == 200)
- 먼저 안드로이드에소 HttpClient 의 객체를 생성한다
- 값을 웹서버로 던지기 위하여 NameValuePair 타입의 ArrayList를 생성 하여 값을 담는다
- 이때 만약에 일정시간내에 응답이 없을시에 timeout를 만들기 위해서는 setConnectionTimeout 메소드를 이용한다.
- UrlEncodedFormEntity 를 이용하여 한글 인코딩한다.
- setEntity로 값을 전송후 reponse 로부터 응답을 기다리며 200일경우는 정상적으로 값전달이 완료 된다.
'프로젝트' 카테고리의 다른 글
AJAX(수정요망) (0) | 2013.07.22 |
---|---|
소스 분석 2) Json파서를 이용하여 값읽어오기. (0) | 2013.07.22 |
zxing(오픈소스) (0) | 2013.07.22 |
개요 (0) | 2013.07.22 |