Android:如何每 10 秒通过服务发送一次 http 请求 [英] Android: How to send http request via service every 10 seconds

查看:22
本文介绍了Android:如何每 10 秒通过服务发送一次 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每 10 秒从服务器接收一次状态.

I need to receive a status from the server every 10 seconds.

我尝试通过服务发送 http 请求来做到这一点.

I tried to do that by sending a http request via service.

问题是我的代码只执行一次.

The problem is that my code executes only once.

这是我的服务的代码:

public class ServiceStatusUpdate extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    while(true)
    {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            new DoBackgroundTask().execute(Utilities.QUERYstatus);
            e.printStackTrace();
        }
        return START_STICKY;            
    }
}

private class DoBackgroundTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String response = "";
        String dataToSend = params[0];
        Log.i("FROM STATS SERVICE DoBackgroundTask", dataToSend);
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(Utilities.AGENT_URL);

        try {
            httpPost.setEntity(new StringEntity(dataToSend, "UTF-8"));

            // Set up the header types needed to properly transfer JSON
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept-Encoding", "application/json");
            httpPost.setHeader("Accept-Language", "en-US");

            // Execute POST
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity responseEntity = httpResponse.getEntity();
            if (responseEntity != null) {
                response = EntityUtils.toString(responseEntity);
            } else {
                response = "{"NO DATA:"NO DATA"}";
            }
        } catch (ClientProtocolException e) {
            response = "{"ERROR":" + e.getMessage().toString() + "}";
        } catch (IOException e) {
            response = "{"ERROR":" + e.getMessage().toString() + "}";
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        Utilities.STATUS = result;
        Log.i("FROM STATUS SERVICE: STATUS IS:", Utilities.STATUS);
        super.onPostExecute(result);
    }
}
}

非常感谢阿维

推荐答案

在 onPostExecute 中放置一个处理程序以在 10 秒后发送 http 请求

Put a handler inside onPostExecute to send a http request after 10 secs

new Handler().postDelayed(new Runnable() {
        public void run() {
            requestHttp();
        }
    }, 10000);

10 秒后 doInBackground 将再次执行,然后 onPostExecute 再次执行,处理程序再次执行,依此类推..

After 10 secs doInBackground will be executed again, after that onPostExecute again, handler again and so on and so on..

这篇关于Android:如何每 10 秒通过服务发送一次 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆