2
Android 애플리케이션에 LongPolling을 구현하려고합니다. Long PollingAndroid Long 폴링 - 서비스 실행 시간 초과
응답을 받기 위해 LongPolling을 오래해야하는 경우 내 Android 서비스가 충돌합니다.
나는 Threads and Async로 시도했다. 일반적으로 나는 많은 직원을 시험해 보았지만, 나는 그것을 얻지 못했다.
public class PollingService extends Service {
String TAG = "AndroidPolling";
int CONNECTION_TIMEOUT = 900000;
int mHeartbeat = 10000;
int TIMEOUT_TOLERANCE = 5000;
String mPushURL = "http://192.168.0.115:8080/de.test.jersey.cti/rest/todos/";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "RestService Service-Class created");
}
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
new makepolling().doInBackground("http://192.168.0.115:8080/de.test.jersey.cti/rest/todos/");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
class makepolling extends AsyncTask<String, String, String> {
String TAG = "AndroidPolling";
int CONNECTION_TIMEOUT = 900000;
int mHeartbeat = 10000;
int TIMEOUT_TOLERANCE = 5000;
String mPushURL = "http://192.168.0.115:8080/de.test.jersey.cti/rest/todos/";
@Override
protected String doInBackground(String... arg0) {
String result = null;
DefaultHttpClient def = new DefaultHttpClient();
HttpParams httpParams = def.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
HttpGet httpGet = new HttpGet(mPushURL);
httpGet.addHeader("Accept", "application/json");
try {
Log.i(TAG, "Executing GET(PUSH) request " + httpGet.getRequestLine());
HttpResponse httpResponse = def.execute(httpGet);
Log.i(TAG, result);
Log.i(TAG, String.valueOf(httpResponse.getProtocolVersion()));
Log.i(TAG, String.valueOf(httpResponse.getEntity().getContent())); //For testing purposes
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
감사합니다. 정말 멋진 기사입니다. – DroidQuelle