【问题标题】:Ion Koush Library - Response is not cacheableIon Koush 库 - 响应不可缓存
【发布时间】:2017-01-17 14:01:10
【问题描述】:

我正在尝试使用 Ion Koush 库从我的后端 API 获取一些 json。 我可以在其中一个 API 路由中成功,但在另一个我有以下答案:响应不可缓存。为了更好地理解问题,这里是代码:

  Ion.with(clientSummaryActivity.this)
      .load("GET",url)
      .setLogging("LOG",Log.VERBOSE)
      .addQuery("format","json")
      .setHeader("Authorization",getIntent().getExtras().getString("token"))
      .asJsonArray()
      .setCallback(new FutureCallback<JsonArray>() {
          @Override
          public void onCompleted(Exception e, JsonArray result) {
              // do stuff with the result or error
              if(e != null){
                  Toast.makeText(clientSummaryActivity.this, "Erro na Query: " + e, Toast.LENGTH_LONG).show(); //cria balao de texto na view indicada
                  Log.v("Query Error2: ",""+e);
                  return;
              }
              if(result == null)
                  Log.v("Error: ","resultado vazio");
              else
                   Log.v("Teste: ",""+result);
              return;
          }
      });
  }

这里是调试日志:

> D/LOG: (0 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: preparing request

>D/LOG: (0 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: preparing request

>I/LOG: (0 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Using loader: com.koushikdutta.ion.loader.HttpLoader@18cbec99

>D/LOG: (0 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Executing request.

>V/LOG: (4 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Resolving domain and connecting to all available addresses

>V/LOG: (10 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: attempting connection to /10.0.2.2:8080

>V/LOG: (14 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: socket connected

>V/LOG: (14 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: 
GET /api/comentario/cliente/00013436415782/?format=json HTTP/1.1
Host: 10.0.2.2:8080
User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; Android SDK built for x86_64 Build/LMY48X)
Accept-Encoding: gzip, deflate
Connection: keep-alive
Accept: */*
Cache-Control: no-cache
Authorization: Token a3e8b50ae73aabad0605433f704eb628269f68c8

>V/LOG: (16 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: request completed

>W/EGL_emulation: eglSurfaceAttrib not implemented
>W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fca6d3b8380, error=EGL_SUCCESS

>D/OpenGLRenderer: endAllStagingAnimators on 0x7fca6d1e4c00 (ListView) with handle 0x7fca6406c080

>V/LOG: (433 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Received headers:

>       HTTP/1.0 200 OK

>       Date: Tue, 17 Jan 2017 13:28:12 GMT

>       Server: WSGIServer/0.1 Python/2.7.12

>       Vary: Accept

>       X-Frame-Options: SAMEORIGIN

>       Content-Type: application/json

>       Allow: GET, HEAD, OPTIONS

>D/LOG: (433 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Response is not cacheable
>V/LOG: (433 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Final (post cache response) headers:

>       HTTP/1.0 200 OK

>       Date: Tue, 17 Jan 2017 13:28:12 GMT

>       Server: WSGIServer/0.1 Python/2.7.12

>       Vary: Accept

>       X-Frame-Options: SAMEORIGIN

>       Content-Type: application/json

>       Allow: GET, HEAD, OPTIONS

>D/LOG: (433 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: Connection successful

>V/LOG: (434 ms) http://10.0.2.2:8080/api/comentario/cliente/00013436415782/?format=json: closing out socket (exception)

这是浏览器 REST 客户端获取的 Json:

  {
    "id_comentario": 5,
    "id_venda_vendas": {
      "id_venda": 139,
      "id_cliente_cliente": {
        "id_cliente": 22,
        "nome": "nome",
        "telefone": "nao_tem_ainda_layout",
        "cpf": "cpf",
        "endereco": "endereco",
        "primeira_vez": true,
        "data_nascimento": "dob",
        "observacao": null,
        "foto": null,
        "email": "nao_tem_ainda_layout",
        "id_ultima_compra": {}
      },
      "id_loja_loja": {
        "id_loja": 2,
        "nome": "nome",
        "nome_fantasia": "nomefantasia",
        "cnpj": "cnpj"
      },
      "data": "2016-11-18",
      "hora": "15:36:02"
    },
    "id_funcionario_funcionario": {
      "id_funcionario": 2,
      "nome": "nome",
      "cargo": "cargo"
    },
    "id_tipo_comentario_tipo_comentario": {
      "id_tipo_comentario": 2,
      "tipo": "Tipo"
    },
    "descricao": "nao tem descricao"
  }
]

关键是,为什么我可以从 REST 客户端获取它,而 Ion 却说 Response 不可缓存?又是什么意思?

OBS:我可以从其他查询中获得成功的结果。不过因为篇幅太长,这里就不贴了。

【问题讨论】:

    标签: android android-ion


    【解决方案1】:

    Ion 自动缓存所有GET 请求。如我所见,您的服务器发送标头Cache-Control: no-cache,意味着不允许缓存此请求。您可以更改服务器上的缓存策略或将.noCache() 添加到您的请求中。

    【讨论】:

    • 您好,感谢您的回答。我已经尝试过这个解决方案,但没有成功。只是为了确认一下,这个设置是在 ion.with 中进行的,对吧?另外,调试日志如下:pastebin.com/w7JNHiYh我正在研究如何在 Django RestFramework 中设置 Headers,也许我可以通过这种方式解决。
    • 嗨,我解决了,我不知道为什么 .noCache() 它对我不起作用,所以我使用了:Ion.getDefault(getApplicationContext()).configure().getResponseCache()。设置缓存(假);现在一切都很好。感谢您的帮助。
    【解决方案2】:

    感谢 Orest Savchak,我可以使用以下方法解决它:

        Ion.getDefault(getApplicationContext()).configure().getResponseCache().setCaching(false);
    

    更多阅读,请查看:

    https://github.com/koush/ion/issues/228

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多