【问题标题】:Websocket enqueue with cache produces a crash when server refuses connection当服务器拒绝连接时,带有缓存的 Websocket 排队会产生崩溃
【发布时间】:2015-11-22 05:44:54
【问题描述】:

设置

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

【问题讨论】:

    标签: android websocket okhttp


    【解决方案1】:

    删除缓存可以解决问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 2020-10-08
      • 2012-03-04
      • 2020-06-26
      • 2022-01-15
      • 2022-06-27
      • 1970-01-01
      相关资源
      最近更新 更多