Long-polling android technology

General Tech Technology & Software 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Technology & Software related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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

profilepic.png
manpreet 2 years ago

 

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.

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

        }

}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.