长轮询android技术
我有一些简单的问题。
我可以只使用AsyncTask对java进行长轮询吗?
class makepolling extends AsyncTask { 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时,我如何再次提出请求?
最好的祝福。 对不起,我的英语不好
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; } } }
您可以在一段时间内执行返回TIME OUT或响应的function
@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; }
- 从List 填充ListView
- Android,添加没有XML布局的新视图
- Android项目中使用的WCF(FileNotFoundException)
- Android setOnClickListener无效
- ListView行按钮:如何创建一个自定义适配器,将View.OnClickListener连接到ListView的每一行上的按钮?
- Android 4.0 – > 4.3(包含) – Webview页面之间的Web存储丢失
- 获取ImageView的图像并将其发送到使用Intent的活动
- LunarLander中的Android IllegalThreadStateException
- getWidth在缩放后不返回不同的值