Background service java.net.SocketTimeoutException: Could not connect after 10 minutes

(sorry for bad english) I am developing an android android / set-top box (STB) app. I have problems with some devices, others work fine. I hope someone has come across something similar or can point me in the right direction.

I use a background service to periodically check some data on our server. I am using retrofit2 with okhttp3 for networking. But after 10 minutes, retrofit starts returning this exception:

IOException java.net.SocketTimeoutException: Could not connect to our.server.org/XX.XX.XXX.XXX(port 80) after 15000ms

My first idea was that this is what it says the server has lost its connection. So I tried to ping from my computer, everything is fine. Then, I checked STB, the connection worked. I am creating another app called Activity, View and Button. When the button is clicked, it sends the same request as this background service. This works without issue. So it looks like there is no problem on the STB side. Therefore, the problem must be service. I am creating it like this:

public class UpdateService extends Service {

    public UpdateService() {

        HandlerThread thread = new HandlerThread("UpdateService:ServiceThread", THREAD_PRIORITY_MORE_FAVORABLE);
        thread.start();

        mServiceLooper = thread.getLooper();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mHandler = new Handler(mServiceLooper);

        final Runnable updateRunnable = new Runnable() {

            public void run() {

                doStuff();
                mHandler.postDelayed(this, UPDATE_CHECK_INTERVAL);
            }

        };
        mHandler.post(updateRunnable);
    }

    private void doStuff() {
        VersionService.getInstance().getVersionList(new VersionService.VersionListener() {
            @Override
            public void onSuccess(ArrayList<VersionObject> data) {
                //code here is called for first 10 minutes 
            }

            @Override
            public void onFailure() {
                //and here after 10 minutes
            }
    }

    public void getVersionList(final VersionListener listener) {

        try {
            Call<VersionListApiResponse> call = ApiClient.getStbApi().getVersionList();
            Response<VersionListApiResponse> response = call.execute();
            if (response.isSuccessful())
            {
                VersionListApiResponse responseObject = response.body();
                listener.onSuccess(responseObject.getVersionList());
            }
        }
        catch (RuntimeException | IOException e)
        {
            listener.onFailure();
        }
    }

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

      

The server request looks like this:

public interface ApiStb {

    @Headers({
            "Accept:application/json",
            "Accept-Charset:UTF-8",
            "Content-type:application/json;charset=utf-8"
    })
    @GET("versionList")
    Call<VersionListApiResponse> getVersionList();
}

      

Am I doing something wrong? I tried to change something. I have used Retrofit in both sync and async. I tried downgrading retrofitting and okhttp (didn't help). I tried to change the return value in onStartCommand. I have exceeded onDestroy () to check if the service is killed (it is not). I tried changing postDelayed (delay) to a different value, but the service stops after 10 minutes every time, it doesn't matter if the delay is 15 or 60 seconds. I have tried restarting the service when I get an exception like:

stopService(new Intent(this, UpdateService.class)); 
startService(new Intent(this, UpdateService.class));

      

but I won't help. The only thing that helps is a hard reboot of the STB, nothing else. After rebooting, it works fine for 10 minutes and then throws exceptions again. This is our own server and the admins have confirmed that there were no new requests after 10 minutes, so it seems like the requests never leave the device. DNS translation works fine (IP address in error message is correct). I tried to change the connection timeout for the network request, but even if I wait 2 minutes, it still throws an exception after that time.

After all this, I tried to install this service on another STB (different type / provider) and there it works as expected. I would really appreciate it if someone could give me advice. I suspected that this device was killing the service for some reason, but onDestroy is never called. Maybe something else?

The affected device is X96 Amlogic S905X Quad Core Android 6.0 TV Box (link: https://www.alibaba.com/product-detail/X96-Amlogic-S905X-Quad-Core-Android_60599604444.html?s=p )

Thank.

EDIT: Possible duplicate / answer not related, I can catch the connection timeout only ok and increase the connection timeout with:

 client.setConnectTimeout(10, TimeUnit.MINUTES);

      

Does not help.

However, I have some new information: I tried to add some other functionality to a service that uses UDP and I am facing the same problem. The service works fine for 10 minutes, but then I keep getting:

java.net.SocketException: sendto failed: EPERM (Operation not permitted)

      

I suppose it has something to do with it? Any help could be helpful.

+3


source to share


1 answer


The solution for the affected device (X96 Amlogic S905X Quad Core Android 6.0 TV Box) was to add at least one action to the Android Manifest. This activity is never shown to the user, but somehow it fixes this issue.



(service starts from receiver with intent filter for android.intent.action.BOOT_COMPLETED)

0


source







All Articles