【问题标题】:Android Long Polling - Timeout executing serviceAndroid Long Polling - 超时执行服务
【发布时间】:2011-04-08 16:55:06
【问题描述】:

我正在尝试在我的 Android 应用程序中实现 LongPolling。 Long Polling

如果 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);
}

}

【问题讨论】:

    标签: android asynchronous comet long-integer polling


    【解决方案1】:

    您不应显式调用 doInBackground()。要启动 AsyncTask,您必须创建它的一个实例并在其上调用 execute() 方法。 This 文章很好地解释了 AsyncTask。

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2010-12-31
      • 2016-11-07
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多