안드로이드 개발 질문/답변
(글 수 45,052)

안드로이드 어플리케이션을 만들고 수집한 데이터를 서버 통신을 통해 저장하고 불러 오려고 합니다.
저 서버 OS는 나중에 회사 DB 에 넣을 때는 자체 서버를 사용하게 되겠지만
개발 중에는 제 개인 PC에 서버를 구축해서 테스트 해보려고 합니다.
(굳이 Oracle DBMS를 쓰는 이유는 회사가 쓰니까 -_-;;)
PC랑 안드로이드간의 통신 방법을 조사하니
1. 자바 소켓 통신 활용 (TCP/IP)
2. HttpClient 활용 (HTTP 프로토콜)
3. 웹서버를 통한 XML 파싱
이렇게 나오는데,
웹서버를 통한 XML 파싱방법을 일반적인 데이터 통신 방법으로 많이들 추천하시네요~
서점을 뒤져가면서 열심히 찾아보았는데,
아무리 봐도 안드로이드 개발 서적 자체에는
어떻게 앱과 통신할 서버를 구축하고 설정을 해줘야 하는지 얘기가 없네요
그냥 무슨 프로토콜을 써서 안드로이드 어플을 개발한다라고 만 있고
통신 예제에도 그냥 연결하고 이렇게 코딩하니 이렇게 된다 라고만 -_-;;;;
다들 DB 연동 없는 앱만 만드는게 아닐텐데....
너무 고민입니다.
정리하면, 외부 DB와 연동되는 안드로이드 앱을 만들고 싶은데,
서버와 DB 구축과 안드로이드와의 서비스 연결 부분을 공부하려면
어떤 책을, 어떤 부분을 보면 되는지 가르쳐주시면 감사하겠습니다!!!!
긴 글 읽어주셔서 감사합니다 ^______^*




위에서 이야기한 방식으로 처리하며 됩니다.
하지만, 처음 구현하는 분이면.. 좀 답답하지요...
우선 심플하게하는 방식으로 2번의 HttpClient방식 좋을 것 같네요..
1번은 소켓처리하는 방식 3번도 XML처리 방식의 사전 지식이 있어야 합니다..
단, 자기가 능력이 있으면.. 인터넷에서 소켓,XML 예제를 찾아 응용해도 됩니다..
공부방법 구글검색에서 android httpclient 소스를 찾으면.... 많은 소스를이 있습니다.
예제소스를 보시면.... 이해가 바로 될 겁니다.
서버쪽은 jsp,asp,php같은 것으로 디비 검색한 결과를 길 스트링으로 작성하여... 안드로이드 폰으로 전송하면 되고요..
클라이언트는 httpclient소스에 보면 받는 부분이 있습니다. 이분에서 길 스트링은 받아 처리하면 됩니다..
자세하게 설명을 하여고하니.... 설명을 못하겠네요... 죄송... 아래 소스는 서버에서 결과값을 받는 소스입니다.. 참고하세요
public class CustomHttpClient {
/** The time it takes for our client to timeout */
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
/** Single instance of our HttpClient */
private static HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the specified url with the
* specified parameters.
*
* @param url The web address to post the request to
* @param postParameters The parameters to send via the request
* @return The result of the request
* @throws Exception
*/
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
//in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "euc-kr"));
StringBuilder sb = new StringBuilder("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
//sb.append(line + NL);
sb.append(line);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
}
}
참고하세요.. HttpPost는 전달값이 있는 것이고. HttpGet그냥 결과값만 받는 겁니다..
수고하세요.(http://eduinfo.pe.kr 에서 안드로이드관련 확인 해보세요.)