Esempio n. 1
0
        public async Task <T> GetObjectAsync(params string[] parameter)
        {
            //先判断网络状态
            string        rootUrl = "http://news-at.zhihu.com/api/4";
            List <string> paras   = new List <string>(parameter);

            paras.ForEach(p => rootUrl += "/" + p);
            string cacheName     = EncryptUtils.GetMd5String(rootUrl);
            string cacheFilePath = $"Cache/{ cacheName}";

            try
            {
                if (await StorageHelper.Instance.StorageItemExistsAsync(cacheFilePath)) //判断是否有缓存await CacheManager.Instance.CacheIsAvailable(cacheName)
                {
                    if (rootUrl.Contains("latest"))                                     //判断是否是今日消息请求
                    {
                        string jsonResult2 = string.Empty;
                        if (!await StorageHelper.Instance.CacheFileIsOutDate(cacheFilePath) || NetWorkHelper.NetWrokState == 0) //判断是否过期
                        {
                            jsonResult2 = await StorageHelper.Instance.ReadTextAsync(cacheFilePath);                            //如果缓存未过期或者没有网络

                            System.Diagnostics.Debug.WriteLine("lastnews---从缓存加载");
                        }
                        else
                        {
                            jsonResult2 = await GetJsonDataAsync(rootUrl);

                            if (!string.IsNullOrEmpty(jsonResult2))
                            {
                                await StorageHelper.Instance.WriteTextAsync(jsonResult2, cacheFilePath, true);
                            }
                        }
                        return(string.IsNullOrEmpty(jsonResult2) ? new T() : JsonConvertHelper.JsonDeserialize <T>(jsonResult2));
                    }
                    else
                    {
                        var jsonResult3 = await StorageHelper.Instance.ReadTextAsync(cacheFilePath);

                        System.Diagnostics.Debug.WriteLine(rootUrl + "---从缓存加载----" + jsonResult3);
                        return(string.IsNullOrEmpty(jsonResult3) ? new T() : JsonConvertHelper.JsonDeserialize <T>(jsonResult3));
                    }
                }
                else
                {
                    var jsonResult4 = await GetJsonDataAsync(rootUrl);

                    if (!string.IsNullOrEmpty(jsonResult4))
                    {
                        await StorageHelper.Instance.WriteTextAsync(jsonResult4, cacheFilePath, true);
                    }
                    return(string.IsNullOrEmpty(jsonResult4) ? new T() : JsonConvertHelper.JsonDeserialize <T>(jsonResult4));
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error at GetObjectAsync:" + ex);
                ExceptionsParameter = "未能获取到数据";
                return(new T());
            }
        }
Esempio n. 2
0
        public async Task <bool> DownloadAsync(IList <string> urls)
        {
            cacheFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Cache");

            foreach (var url in urls)
            {
                string fileName = EncryptUtils.GetMd5String(url);
                //如果文件不存在或者为空文件,则开始下载
                if (!await StorageHelper.Instance.StorageItemExistsAsync("Cache/" + fileName))
                {
                    System.Diagnostics.Debug.WriteLine("Begin Cache Pic");
                    var storageFile = await cacheFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
                    await SaveImage(url, storageFile);
                }
            }
            return(true);
        }
Esempio n. 3
0
        public async void BeginDownloadAsync(IList <string> urls)
        {
            cacheFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("Cache");

            urlList.AddRange(urls);
            for (int i = 0; i < urlList.Count; i++)
            {
                string fileName = EncryptUtils.GetMd5String(urlList[i]);

                var storageFile = await cacheFolder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

                //如果文件不存在或者为空文件,则开始下载
                if (!await StorageHelper.Instance.StorageItemExistsAsync(fileName))
                {
                    await SaveImage(urlList[i], storageFile);
                }
                if (ImgLoadedProcess != null)
                {
                    ImgLoadedProcess(urlList[i], storageFile);
                }
            }
        }