Okhttp : Asynchronous request doesn’t stop immediately after onResponse

First sorry for my English which is not my native language.

I use okhttp to do some simple asynchronous calls but my program doesn’t stop immediately after the call of onResponse. It takes some seconds and then stops. I don’t have this issue on Android 5 but on my desktop. I have the same issue with others URLs. Maybe there is something I did wrong. The request is performed in another thread. My network is under a proxy.

I use : okhttp-2.4.0 and okio-1.4.0 and java8.

Redirect me if this issue was already answered.

This my code : `

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

    Settings.setProxy();
    Request request = new Request.Builder()
            .url("http://openlibrary.org/search.json?q=la+famille")
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            e.printStackTrace();
            System.out.println("error");
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            System.out.println(response.body().string());
            System.out.println("coucou");
        }
    });
}

`

Answer

As you find out from the issue when you do an async call an ExecutorService is created and the pool is keeping the VM alive.

To shutdown the pool just get the dispatcher and close it:

client.getDispatcher().getExecutorService().shutdown();