在后台android服务中异步HTTP获取请求(背景数据同步)

时间:2018-12-05 15:51:19

标签: android background http-get data-synchronization android-async-http

我正在尝试进行正确的后台数据同步。即使配置了HTTP超时,有时请求仍会发送到服务器,有时会卡住并且长时间没有响应的问题。

我已经在Manifest中注册了服务,

    <service
        android:name=".service.JobSchedulerService"
        android:exported="true"
        android:permission="android.permission.BIND_JOB_SERVICE" />

服务:

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.util.Timer;
import java.util.TimerTask;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.message.BasicHeader;

public class JobSchedulerService extends Service {

    public class LocalBinder extends Binder {
        JobSchedulerService getService() {
            return JobSchedulerService.this;
        }
    }

    private final IBinder mBinder = new LocalBinder();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        timer.cancel();
        return true;
    }

    private static Timer timer;

    public AsyncHttpClient getHttpClient() {
        //in real this method extracted as a singleton in extra class
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setURLEncodingEnabled(true);
        asyncHttpClient.setTimeout(5000);
        asyncHttpClient.setConnectTimeout(5000);
        asyncHttpClient.setResponseTimeout(5000);

        return asyncHttpClient;
    }

    @Override
    public void onCreate() {

        timer = new Timer();
        final Handler mHandler = new Handler(Looper.getMainLooper());
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(new Runnable() {
                    public void run() {
                        startSynchronisation();
                    }
                });
            }

        }, 0, 15000);
    }

    private void startSynchronisation() {

        AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //some parsing
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //some messages
            }
        };

        String url = "http://some_url.com";
        Header[] headers = {
                new BasicHeader("Accept", "application/json"),
        };

        getHttpClient().get(JobSchedulerService.this, url, headers, null, responseHandler);

    }

}

当我调试此代码时,数据请求正在执行,没有任何异常和错误,但是从不返回值,甚至从未获取TimeoutException。

有人知道我错过了什么吗?一些属性或其他电话?

或者也许有人可以推荐一种在后台实现数据同步的正确方法?即使关闭应用程序,也无法阻止。

0 个答案:

没有答案