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.
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;
}
}
}
You can do a function into a while that return TIME OUT or your response
@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;
}
来源:https://stackoverflow.com/questions/20800189/long-polling-android-technology