안녕하세요

메인 쓰레드에서 버튼을 누르면, 리스너에서 asynctask 또는 쓰레드를 하나 생성해서

내부에서 http 통신으로 서버에 있는 디비에 값을 하나 송신하는 기능을 구현중인데 문제가 생겨서 질문드립니다.


버튼을 약 0.5~1초 이성 간격으로 누르면 누를때마다 모든 값이 문제없이 업데이트 되나,

버튼을 빠른속도로 연타할 경우에, 가끔 값이 누락되는 경우가 발생합니다.

http 프로토콜 자체의 문제로 판단되어, 다른 해결책을 찾는 중인데ㅡ


제가 생각한 해결책은

1. http 연결 방식이 아닌 다른 프로토콜(소켓, udp )등의 방식으로 연결

2. 버튼을 누를때마다 쓰레드 호출이 아니라 , 백그라운드 서비스를 하나 생성해서

   첫 입력이 발생하면 서비스를 호출하여 약 1분 또는 10분 이런식으로 버튼을 누를때마다 서버와 통신하는 것이 아니라

  시간주기마다 변하는 값들을 모아서 서버에 전송하고, 1분동안 입력이 없으면 자동으로 sleep 상태에 들어가는 

   서비스를 구현하는 방식 이 두 가지인데,


다른 고수님들의 의견을 듣고싶습니다. 혹시나 몰라 제가 사용한 http 연결 소스를 붙입니다.

버튼을 누를때마다 asynctask에서 아래 sendSchedule함수를 호출하여 값을 서버로 전송합니다.


static public boolean sendSchedule( String id, String sendData){ //전체 list 받아오기

URL mURL=null;

HttpURLConnection mHURLConn = null;

String resultOfQuery = new String();

int mIntResponse=0;

String mStrUrl="";


String month[] = sendData.split("/"); // 12/05/12/08/1

String timedata= month[0]+month[1]+month[2]+month[3];

mStrUrl="http://testserver.com/maru/shop/schedule.php?id=";

id = URLEncoder.encode(id);  //id와 password는 한글일 수 있기 때문에 인코딩해서 보낸다.

mStrUrl = mStrUrl + id + "&schedule="+ timedata + "&status=" + month[4];


Log.d("[SEND]", "sendData Url: "+ mStrUrl);


try{

mURL = new URL("mStrUrl); //해당 URL

mHURLConn = (HttpURLConnection)mURL.openConnection();

mHURLConn.setConnectTimeout(10000); //max timeout == 10sec

mHURLConn.setUseCaches(false);

mIntResponse = mHURLConn.getResponseCode();

}

catch(Exception e)

{

mIntResponse = 1000;

}


if(mIntResponse == HttpURLConnection.HTTP_OK) //연결 검사

{

BufferedReader br = null;

try {

br = new BufferedReader(new InputStreamReader(mHURLConn.getInputStream()));

for (;;)

{

String line = null;

try {

line = br.readLine();

} catch (IOException e) {

e.printStackTrace();

}

if (line == null) break;

resultOfQuery +=line;

}

br.close();

} catch (IOException e) {

e.printStackTrace();

}

//Log.d("test","value:"+ resultOfQuery);

return true;

}

return false;

}