2018-03-07 15:46
浏览 101


My application get JSON data from server. If I use my own json api , data is loaded only in internet connection. But if I use this api (, data is displayed also in offline, once application got internet connection.

The same code, one json url is loaded also after the loss of internet connectivity, another one's data doesn't display after internet disconnection. How to store data in android cache from server-side as this URL (

My api (api1.php)

header('Content-Type: application/json');

    $query="SELECT * FROM implink";
$result = $con->query('SELECT id,heading,content FROM implink');
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json;charset=utf-8');
echo json_encode(['info' => $rows],
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongrendang6566
    dongrendang6566 2018-03-07 16:17

    My first suggestion is to not publish your API key. Pixabay may be free, but sharing API keys on the internet will put you infrastructure in danger.

    Upon looking up the request it seems Pixabay is setting some headers that could affect your cache.

    Access-Control-Max-Age → 86400
    Cache-Control → max-age=86400
    Content-Type → application/json

    Check out the Android documentation on HttpResponseCache It outlines how you can force a cache response after setting up the HttpResponseCache.

         try {
             connection.addRequestProperty("Cache-Control", "only-if-cached");
             InputStream cached = connection.getInputStream();
             // the resource was cached! show it
         } catch (FileNotFoundException e) {
             // the resource was not cached
    点赞 评论