Php 如何在android中存储json数据以实现离线

Php 如何在android中存储json数据以实现离线,php,android,json,api,pixabay,Php,Android,Json,Api,Pixabay,我的应用程序从服务器获取JSON数据。如果使用,则仅在internet连接中加载数据。但如果我使用此api(),数据也会在应用程序连接到internet后脱机显示 同样的代码,一个json url在失去互联网连接后也会加载,另一个的数据在互联网断开后不会显示。如何将服务器端的数据作为此URL()存储在android缓存中 我的api(api1.php) 我的第一个建议是不要发布API密钥。Pixabay可能是免费的,但在互联网上共享API密钥将使您的基础设施处于危险之中 在查找请求时,Pixab

我的应用程序从服务器获取JSON数据。如果使用,则仅在internet连接中加载数据。但如果我使用此api(),数据也会在应用程序连接到internet后脱机显示

同样的代码,一个json url在失去互联网连接后也会加载,另一个的数据在互联网断开后不会显示。如何将服务器端的数据作为此URL()存储在android缓存中

我的api(api1.php)


我的第一个建议是不要发布API密钥。Pixabay可能是免费的,但在互联网上共享API密钥将使您的基础设施处于危险之中

在查找请求时,Pixabay似乎正在设置一些可能影响缓存的头

Access-Control-Max-Age → 86400
Cache-Control → max-age=86400
Content-Type → application/json
查看上面的Android文档,其中概述了如何在设置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
     }
     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
     }