So I'm trying to get a Json string from a Url in a windows phone 8 app. I just have to call a callbackurl that returns this string, and that's pretty much it, but somehow I've been stuck on this for days and I just don't understand how to do it
I have a urlparser class that contains 2 methods which are :
public void ParseJsonUrl(string url)
{
Uri uri = new Uri(url);
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
webClient.DownloadStringAsync(uri);
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var jsonData = JsonConvert.DeserializeObject<parameter>(e.Result);
Debug.WriteLine(jsonData.parameter1);
}
For now I'm just trying to display one of the parameters contained in my Json string, of course my methods will perform other things once I get this working
I have a class called "parameters" at the beginning of my urlparser.cs file which looks like
public class parameter
{
public string parameter1 { get; set; }
public string parameter2 { get; set; }
public string parameter3 { get; set; }
}
But this doesn't work... I get this error
'System.Reflection.TargetInvocationException'
I followed this tutorial http://blogs.msdn.com/b/pakistan/archive/2013/06/23/10425845.aspx and saw loads of other ones that are pretty much the same thing, but unfortunately, this doesn't work. In some tutorials they use "DownloadString" instead of "DownloadStringAsync" but I can't call this method (maybe not available with WP8), and in some other tutorials they use "await" in the method but I can't understand where I should place the "await" statement and what other pieces of code I should add
Furthermore, once I will be able to get my json data in my var, if someone can tell me how to access it from another class, it would be great !
Thanks !
DownloadStringAsyncreturns the string if you justawaitfor it.