Я пытаюсь реализовать LongPolling в моем приложении для Android. Long Polling
Если LongPolling нужно долго ждать ответа, мой сервис Android падает.
Я пробовал с Threads и 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);
}
}