Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Technology & Software 2 years ago
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.
Turn Your Knowledge into Earnings.
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 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.
AsyncTasks
Service
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; } } }
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.