我正在尝试使用异步 http 客户端Http
Get
在服务内 x 秒后发送心跳请求
里面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;
}
executeSample
每 5 秒调用一次,但它给了我一个 execption
09-09 16:52:07.712:E/exception(21720):executeSample 失败异常:09-09 16:52:07.712:E/exception(21720):java.lang.IllegalArgumentException:AsyncHttpClient 中使用的同步 ResponseHandler。您应该在 looper 线程中创建响应处理程序,或者改用 SyncHttpClient。09-09 16:52:07.712: E/exception(21720): 在 com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1165) 09-09 16:52:07.712: E/exception(21720) : 在 com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:942) 09-09 16:52:07.712: E/异常(21720): 在 com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient .java:925) 09-09 16:52:07.712: E/exception(21720): 在 com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:900) 09-09 16:52:07.712: E /异常(21720):在 com.services。
任何建议/帮助
问候