使用 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?
干杯乔赫
推荐答案
我就是这样解决的.首先设置文件路径,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 文件加载到本地浏览器存储中.像这样:
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 调用(查看 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屋!