Android长轮询-执行服务超时
我正在尝试在我的 Android 应用程序中实现 LongPolling。 长轮询
如果长轮询需要很长时间才能得到答案,我的 Android 服务就会崩溃。
我尝试使用线程和异步。一般来说,我尝试了很多工作人员,但我没有得到它。
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);
}
}
I am trying to implement LongPolling in my Android Application. Long Polling
If the LongPolling needs to long to get a answer my android service crashes.
I tried with Threads and Async. Generally i tried a lot of staff, but i don`t got it.
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应显式调用 doInBackground()。要启动 AsyncTask,您必须创建它的实例并调用它的execute() 方法。 这篇文章很好地解释了 AsyncTask。
You should not call doInBackground() explicitly. To start an AsyncTask, you must create an instance of it and call the execute() method on it. This article explains AsyncTask nicely.