长轮询android技术

时间:2013-12-27 11:48:39

标签: java android long-polling

我有一些简单的问题 我可以只使用AsyncTask对java进行长轮询吗?

class makepolling extends AsyncTask<String, String, String> {

    String TAG = "AndroidPolling";
    int CONNECTION_TIMEOUT = 900000;
    int mHeartbeat = 10000;
    int TIMEOUT_TOLERANCE = 5000;
    String mPushURL = "https://my_serv_adress/service";

    @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);
        HttpPost httpPost = new HttpPost(mPushURL);
        httpPost.addHeader("Accept", "application/json");

        try {
            Log.i(TAG, "Executing POST(PUSH) request " + httpPost.getRequestLine());

            HttpResponse httpResponse = def.execute(httpPost);
            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;

    }

当响应返回TIME OUT时,我如何再次提出请求?
最好的祝福。 抱歉我的英文不好

2 个答案:

答案 0 :(得分:6)

AsyncTasks旨在用于相对较短的操作,因此如果您要进行一些长时间的轮询,您应该尝试不同的方法。如果您想定期拨打网络电话,可以在后台运行Service

以下代码可能对您有所帮助。它只是一个每10秒执行一次的服务模板。请记住,网络调用需要在UI线程之外完成:

public class MyService extends Service {

        private IBinder mBinder = new SocketServerBinder();
        private Timer mTimer;
        private boolean mRunning = false;

        @Override
        public void onCreate() {
                super.onCreate();
                mTimer = new Timer();
                mTimer.schedule(new TimerTask() {

                        @Override
                        public void run() {

                                if (mRunning) {
                                        // make your network call here
                                }
                        }
                }, 10000, 10000);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                mRunning = true;
                return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent arg0) {
                mRunning = true;
                return mBinder;
        }

        @Override
        public boolean onUnbind(Intent intent) {
                mRunning = false;
                return super.onUnbind(intent);
        }

        public class SocketServerBinder extends Binder {

                public MyService getService() {
                        return MyService.this;
                }

        }

}

答案 1 :(得分:3)

您可以在一段时间内执行返回TIME OUT或响应的功能

 @Override
    protected String doInBackground(String... arg0) {
     String result= TIME_OUT;   //public static final String TIME_OUT = time_out_error" 
     while(result.equals(TIME_OUT))
         result = getServerInformation();

     return result;
    }




public String  getServerInformation(){
         String result = null;
        DefaultHttpClient def = new DefaultHttpClient();
        HttpParams httpParams = def.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);

        ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpPost httpPost = new HttpPost(mPushURL);
        httpPost.addHeader("Accept", "application/json");

        try {
            Log.i(TAG, "Executing POST(PUSH) request " + httpPost.getRequestLine());

            HttpResponse httpResponse = def.execute(httpPost);
            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();
        }
//HERE YOU SHOULD TURN result = TIME_OUT or whatever you want
        return result;

  }
相关问题