Okhttp:onResponse之后异步请求不会立即停止

Moh*_*ARA 5 java okhttp

首先,抱歉我的英语不是我的母语。

我使用okhttp进行一些简单的异步调用,但是在onResponse调用之后,我的程序并没有立即停止。需要几秒钟,然后停止。我在Android 5上没有这个问题,但是在我的桌面上。我对其他网址也有同样的问题。也许我做错了什么。该请求在另一个线程中执行。我的网络在代理下。

我使用:okhttp-2.4.0okio-1.4.0java8

如果此问题已得到解决,请重定向我。

这是我的代码:

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");
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

`

Enr*_*man 5

正如您从执行异步调用时的问题中发现的那样,将创建 ExecutorService 并且池使 VM 保持活动状态。

要关闭池,只需获取调度程序并关闭它:

client.getDispatcher().getExecutorService().shutdown();
Run Code Online (Sandbox Code Playgroud)