当服务器拒绝连接时,带缓存的Websocket入队会导致崩溃

时间:2015-08-27 17:48:56

标签: android websocket okhttp

设置

File cacheDirectory = new File(app.getCacheDir(), "network_cache");
        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdir();
        }
        Cache cache = new Cache(cacheDirectory, CACHE_MAX_SIZE);
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.networkInterceptors().add(new StethoInterceptor());
        okHttpClient.setCache(cache);

导致此异常,不可能被捕获:

Process: xx.xx.xx.debug, PID: 8227
    java.lang.NullPointerException: Attempt to invoke virtual method 'okio.BufferedSource com.squareup.okhttp.ResponseBody.source()' on a null object reference
            at com.squareup.okhttp.internal.http.HttpEngine.cacheWritingResponse(HttpEngine.java:935)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:821)
            at com.squareup.okhttp.Call.getResponse(Call.java:268)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
            at com.squareup.okhttp.Call.access$100(Call.java:34)
            at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
            at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

这发生在okhttp 2.5和okhttp-ws 2.5上。使用okhttp 2.4和okhttp-ws 2.4,我们遇到了没有堆栈跟踪的崩溃

创建问题:https://github.com/square/okhttp/issues/1826

1 个答案:

答案 0 :(得分:0)

删除缓存修复了问题

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.networkInterceptors().add(new StethoInterceptor());