每X秒内部服务后发送异步Http获取请求

时间:2014-09-09 11:55:59

标签: android android-async-http

我尝试使用async http client

在服务中x秒后发送心跳Http Get请求

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看起来像

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;
        }
每隔5秒就会调用<{executeSample,但它会给我一个执行

  

09-09 16:52:07.712:E / exception(21720):executeSample失败   例外:09-09 16:52:07.712:E /例外(21720):   java.lang.IllegalArgumentException:使用了同步ResponseHandler   在AsyncHttpClient中。您应该在a中创建响应处理程序   looper线程或使用SyncHttpClient代替。 09-09 16:52:07.712:   E /例外(21720):at   com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1165)   09-09 16:52:07.712:E /例外(21720):at   com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:942)   09-09 16:52:07.712:E /例外(21720):at   com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:925)   09-09 16:52:07.712:E /例外(21720):at   com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:900)   09-09 16:52:07.712:E /例外(21720):at   com.services.HeartBeatService.executeSample(HeartBeatService.java:90)   09-09 16:52:07.712:E /例外(21720):at   com.services.HeartBeatService $ 1.run(HeartBeatService.java:55)09-09   16:52:07.712:E /例外(21720):at   java.util.Timer中$ TimerImpl.run(Timer.java:284)

任何建议/帮助

此致

0 个答案:

没有答案