Reputation: 1714
I am working on a weather application, So I have to get icons (like clouds) and other information from some API. Here, I am using OpenWeatherMap API. (This part is easy)
But the problem is that, if the internet is not available, then how do I display the previous data?.
It may retrieve icons and data from some source in the phone, maybe be a sqlite database, or JsonStore or DictStore or any other efficient way if possible.
Storing image/icon in sqlite is a real pain and also I have to keep on changing the icons everytime application is refreshed.(So storing and retrieving from database won't be a good idea.)
I don't know how to save icons/images in JsonStore/DictStore. (by base64 maybe)
Also found this link but not much helpful.
Any advice or example is welcome.
Upvotes: 0
Views: 1534
Reputation: 1714
I figured it out by myself. This answer is for future references.
What I did was, saved all the data that I got from that API(which was already in JSON format) into a json
file.
To write in file weather.json
import json
from urllib import urlopen
url = urlopen('http://api.openweathermap.org/data/2.5/forecast/daily?q={}&mode=json&units={}'.format(getname,temp_type)).read()
#where getname is the name of city.
#and temp_type is either C(Celsius) or F(Fahrenheit)
result = json.loads(url)
out_file = open("weather.json","w")
json.dump(result,self.out_file, indent=4)
#indent = 4, just to make it easy to read.
out_file.close()
And to read from file weather.json
in_file = open("weather.json", "r")
result = json.load(self.in_file)
in_file.close()
And for the icons I used requests
module and saved each icon with a unique name, then everytime the user did a new search or refresh the application then automatically the file would be updated and new icons would be downloaded and replaced with the existing ones.
import requests
conditions_image1 = "http://openweathermap.org/img/w/{}.png".format(result['list'][1]['weather'][0]['icon'])
#or whatever be the name of your image
response1 = requests.get(conditions_image1)
if response1.status_code == 200:
f = open("./icons/wc1.png", 'wb')
f.write(response1.content)
f.close()
And also as I am using kivy
, So I would like to mention that you need to add json
in buildozer.spec
file (As you might have tried it in your PC first)
source.include_exts = py,png,jpg,kv,atlas,json
Upvotes: 2
Reputation: 6899
Though I have not tried, but I think you can follow the below steps to achieve this.
As you are using the OpenWeatherMap API, so I assume you have implemented a method named getImage(String code)
which would return the icon in byte[] (As mentioned in the example here).
Upvotes: 1
Reputation: 1387
I would really consider using a hosted database in this situation, with with a combination of using Picasso(image caching) so once the icon received from the database it's cached and used for another time. Sqlite can be used too but storing the icons will a huge pain as you'll have to use a blob which isn't really efficient
Upvotes: 0