2

I'm trying to send a heartbeat Http Get request after x seconds inside a service using async http client

inside onStartCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.v(TAG,"actual heartbeat service started");
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {                   
                Log.v(TAG,"sending heart beat");
                addRequestHandle(executeSample( getAsyncHttpClient(),
                        getUrlText(),
                        getRequestHeaders(),
                        getRequestEntity(),
                        getResponseHandler())
        );              
                 }
            }, 0, 5000);    
    return Service.START_STICKY;    
}

executeSample looks like

public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
         Log.v(TAG,"heartbeat service : executeSample");
            try 
            {
                Log.v(TAG,"executeSample");                 
                RequestParams params = new RequestParams();           
                params.put("test","test");
                return client.get(URL, responseHandler);                    
            } catch (Exception fnfException) {                  
            }
            return null;
        }

executeSample is called after every 5 seconds but it gives me an execption

09-09 16:52:07.712: E/exception(21720): executeSample failed exception: 09-09 16:52:07.712: E/exception(21720): java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead. 09-09 16:52:07.712: E/exception(21720): at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1165) 09-09 16:52:07.712: E/exception(21720): at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:942) 09-09 16:52:07.712: E/exception(21720): at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:925) 09-09 16:52:07.712: E/exception(21720): at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:900) 09-09 16:52:07.712: E/exception(21720): at com.services.HeartBeatService.executeSample(HeartBeatService.java:90) 09-09 16:52:07.712: E/exception(21720): at com.services.HeartBeatService$1.run(HeartBeatService.java:55) 09-09 16:52:07.712: E/exception(21720): at java.util.Timer$TimerImpl.run(Timer.java:284)

any suggestion/help

Regards

Dakait
  • 2,531
  • 1
  • 25
  • 43
  • Check this http://stackoverflow.com/questions/19617621/android-how-to-send-http-request-via-service-every-10-seconds – Deniz Sep 09 '14 at 12:12
  • @deniz tnx for your effort i have already gone through the link, i want to make async requests using http async client... – Dakait Sep 09 '14 at 12:42
  • check my answer I had this problem too http://stackoverflow.com/a/38927364/586687 – angel Aug 12 '16 at 22:41

0 Answers0