本文介绍了长轮询android技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题.
我可以仅使用AsyncTask在Java上进行长时间轮询吗?

I have some easy question.
Can I make long polling on java, using only AsyncTask?

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;

    }

当响应返回超时时,我该如何再次提出请求?
最好的祝福.对不起,我的英语不好

when responce return TIME OUT, how i make request again?
Best regards.sorry for my bad english

推荐答案

AsyncTasks用于较短的操作,因此,如果要进行长时间轮询,则可能应该尝试其他方法.如果要定期进行网络呼叫,可以在后台运行Service.

AsyncTasks are meant to be used for relatively short operations, so if you're looking to do some long polling, you should probably try a different approach. If you want to periodically make a network call, you could have a Service running in the background.

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

The following code might help you. It's just a template of a Service that gets executed every 10 seconds. Remember that the network call needs to be done off the UI thread:

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;
                }

        }

}

这篇关于长轮询android技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:04