Android长轮询-执行服务超时

发布于 2024-10-31 06:02:43 字数 2278 浏览 3 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-11-07 06:02:43

您不应显式调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文