kiok46
kiok46

Reputation: 1714

Save app data in Weather App

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

Answers (3)

kiok46
kiok46

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

Sush
Sush

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).

  1. Step 1: After you call getImage (when the app can access internet), write the byte[] to a file in the internal/external storage and name it same as the WeatherId or give some unique name to the file. You can refer the link to be able to write to internal/external storage.
  2. Step 2: Write all the weather information you want to be able to get when the device is offline to a sqlite database. In the icon column, just put the name of the file you have created in step 1.
  3. Step 3: When offline and you want to retrieve the weather information, read the data from the sqlite DB, get the icon file name from the weather record and read the file content to get the icon as byte[] and use it to show the icon.

Upvotes: 1

Errol Green
Errol Green

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

Related Questions