下载文件和本地存储他们的PhoneGap / jQuery Mobile的Android和iOS应用 [英] Download files and store them locally with Phonegap/jQuery Mobile Android and iOS Apps
问题描述
我写了jQuery Mobile的应用程序,并与PhoneGap的包装到iOS和Android应用程序。
I wrote an jQuery Mobile app and packaged it with Phonegap to iOS and Android apps.
在这一点上,我使用本地存储的JSON文件读取数据。
At this point I am using locally stored json files to read data.
我想通过从服务器下载新的JSON文件来随时更新这些JSON文件的时间。
I would like to update these json files from time to time by downloading newer json files from a server.
我怎样才能从服务器的JSON和JSON文件存储到Android和iOS的本地文件系统?
How can I get the json from the server and store the json files to the local file system of Android and iOS?
干杯 Johe
推荐答案
这是我如何解决它。首先设置文件路径,至极对于Android和iOS不同的
This is how I solved it. First set the file paths, wich are different for Android and iOS
var file_path;
function setFilePath() {
if(detectAndroid()) {
file_path = "file:///android_asset/www/res/db/";
//4 Android
} else {
file_path = "res//db//";
//4 apache//iOS/desktop
}
}
然后我打开我的JSON文件,至极的ppackaged与应用程序$ P $,到本地浏览器存储。像这样的:
Then I load my JSON files, wich are prepackaged with the app, into the local browser storage. Like this:
localStorage["my_json_data"] = loadJSON(file_path + "my_json_data.json");
function loadJSON(url) {
return jQuery.ajax({
url : url,
async : false,
dataType : 'json'
}).responseText;
}
如果我想更新我的数据。我从服务器获取新的JSON数据,并将其推入本地存储。如果服务器是在不同的领域,这是大多数的时候,你必须做出一个JSONP调用(检查jQuery的文档上的 JSONP )。 我这样做是有点这样的:
If I wanna update my data. I get the new JSON Data from the server and push it into the local storage. If the server is on a different domain, which is the case most of the time, you have to make a JSONP call (check jQuery's docs on JSONP). I did it kinda like this:
$.getJSON(my_host + 'json.php?function=' + my_json_function + '&callback=?', function (json_data) {
//write to local storage
localStorage["my_json_data"] = JSON.stringify(json_data);
});
这篇关于下载文件和本地存储他们的PhoneGap / jQuery Mobile的Android和iOS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!