Android Retrofit okhttp错误

时间:2015-09-30 21:13:13

标签: android retrofit okhttp

我正在使用Retrofit和okhttp在我的应用中进行网络请求。在尝试发出登录请求时,我发现当它是全新安装时,它可以正常工作。我收到了200响应但是当我尝试注销并重新登录时,我在日志中得到了这个例外。有人看到这样的问题。请帮忙。

java.lang.IllegalStateException: cache is closed
D/Retrofit:     at com.squareup.okhttp.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:630)
D/Retrofit:     at com.squareup.okhttp.internal.DiskLruCache.get(DiskLruCache.java:424)
D/Retrofit:     at com.squareup.okhttp.Cache.get(Cache.java:184)
D/Retrofit:     at com.squareup.okhttp.Cache$1.get(Cache.java:139)
D/Retrofit:     at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:226)
D/Retrofit:     at com.squareup.okhttp.Call.getResponse(Call.java:267)
D/Retrofit:     at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
D/Retrofit:     at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
D/Retrofit:     at com.squareup.okhttp.Call.execute(Call.java:79)
D/Retrofit:     at retrofit.client.OkClient.execute(OkClient.java:53)
D/Retrofit:     at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
D/Retrofit:     at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
D/Retrofit:     at java.lang.reflect.Proxy.invoke(Proxy.java:397)
D/Retrofit:     at $Proxy3.storeList(Unknown Source)

我解决了这个错误。我没有设置这个" ref"当我注销并关闭磁盘时为null。因此,当我重新登录时,它会导致异常。

RestAdapter restAdapter = new RestAdapter.Builder()
            .setConverter(new GsonConverter(new Gson()))
            .setExecutors(newAsyncExecutor(), useAsyncCallbacks ? newAsyncExecutor() : newMainThreadExecutor())
            .setClient(getRetrofitClient())
            .setEndpoint(serverUrl)
            .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade requestFacade) {
                    requestFacade.addHeader("Accept", "application/json");
                }
            });
MyClass ref = restAdapter.create(MyClass.class);

1 个答案:

答案 0 :(得分:1)

以下是DiskLruCache类的问题:

  /** Returns true if this cache has been closed. */
  public synchronized boolean isClosed() {
    return closed;
  }

  private synchronized void checkNotClosed() {
    if (isClosed()) {
      throw new IllegalStateException("cache is closed");
    }
  }

所以我认为,为了更好地为您提供如何处理导致错误的情况的答案,您应该在发生此错误的位置发布代码。