首先,对不起,我的英语不是我的母语。
我使用okhttp进行一些简单的异步调用,但是我的程序不会在onResponse调用之后立即停止。需要几秒钟才能停下来。我在Android 5上没有这个问题,但在我的桌面上。我对其他URL也有同样的问题。也许我做错了什么。请求在另一个线程中执行。我的网络被代理了。
我使用:okhttp-2.4.0、okio-1.4.0和java8。
如果这个问题已经解决了,请重新引导我。
这是我的代码:
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");
}
});
}`
发布于 2015-12-01 06:56:25
正如您从问题中发现的,当您执行异步调用时,将创建一个ExecutorService,并且该池将保持VM活动。
要关闭池,只需获取调度程序并关闭它:
client.getDispatcher().getExecutorService().shutdown();发布于 2021-04-07 14:26:18
我不知道怎么回事,但这句话对我有用,client.connectionPool().evictAll();
https://stackoverflow.com/questions/31183730
复制相似问题