下载/反序列化JSON文件

时间:2014-08-07 23:10:11

标签: c# windows-phone-8 windows-phone-8.1

我尝试下载和反序列化JSON文件。

我有这段代码但是当我运行我的应用程序时,我不会下载我的文件

public static async void btnGet_Tap()
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        try
        {
            Uri source = new Uri("http://88.174.101.185:15343/share/CkEtT9llw2BCbmPL/all-cards.json");
            StorageFile destinationFile = await localFolder.CreateFileAsync(
                "all-cards.json", CreationCollisionOption.GenerateUniqueName);

            BackgroundDownloader downloader = new BackgroundDownloader();
            DownloadOperation download = downloader.CreateDownload(source, destinationFile);
            System.Diagnostics.Debug.WriteLine("-- Download OK");

            // Getting JSON from file if it exists, or file not found exception if it does not  

            StorageFile storageFile = await localFolder.GetFileAsync("all-cards.json");
            string configData = await FileIO.ReadTextAsync(storageFile);


            // deserialize back to our product!  
            System.Diagnostics.Debug.WriteLine(" -- Read OK : " + configData);
            RootObject card = JsonConvert.DeserializeObject<RootObject>(configData);
            // and show it   
            System.Diagnostics.Debug.WriteLine("Deserialization OK" + card.meta.patch);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.StackTrace);
        }
    }
}

我的错误:

-- Download OK
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
 -- Read OK : 
A first chance exception of type 'System.NullReferenceException' occurred in App3.exe
   at App3.Control.<btnGet_Tap>d__0.MoveNext()
The program '[2364] App3.exe' has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:0)

您的服务器提供404响应,因此您实际上无法获得正确的数据。