Getting JSON string from url on Windows Phone 8

So, I'm trying to get a Json string from Url in a Windows Phone 8 app. I just need to call callbackurl that returns that string and that's pretty much it, but somehow I'm stuck on this for days and I just don't get it , how to do it.

I have a urlparser class that contains 2 methods:

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);
    }

      

While I'm just trying to display one of the parameters contained in my Json string, of course my methods will do other things as soon as I get this working

I have a class called "parameters" at the beginning of my urlparser.cs file that looks like

public class parameter
{
    public string parameter1 { get; set; }
    public string parameter2 { get; set; }
    public string parameter3 { get; set; }
}

      

But it doesn't work ... I am getting this error

'System.Reflection.TargetInvocationException'

I followed this tutorial http://blogs.msdn.com/b/pakistan/archive/2013/06/23/10425845.aspx and saw many others that are almost the same, but unfortunately it doesn't work. In some tutorials they use "DownloadString" instead of "DownloadStringAsync" but I cannot call this method (probably not available since WP8) and in some other tutorials they use "wait" in the method but I cannot figure out where I am should put a "wait" statement and what other pieces of code should i add

Also, once I can get my json data in my var, if someone can tell me how to access it from another class, that would be great!

Thank!

+3


source to share


3 answers


So after figuring out what the problem is, I thought I'd answer the question if anyone has one problem. The thing is, I had the code in my "ParseJsonUrl" function after the line

webClient.DownloadStringAsync(uri);

      

And that was the problem. Also, ParseJsonUrl is called by the function called by the button, and after this call, the function also makes other calls and the button too.

And all the thoose calls result in the line never being loaded until EVERYTHING called by the button is done and I needed the line before.

The solution is to create a DownloadStringCompleted method in the class that calls my UrlParser.ParseJsonUrl function, and pass that event to the function in its arguments.

Then all that has to be done on the click of the button is set in that the DownloadStringCompleted method

What the code looks like:



private void Button_Click(object sender, RoutedEventArgs e)
    {
        MyClass myClass = new MyClass();
        myClass.Function(url); // Assuming url is already set somewhere
    }

      

Then in MyClass

public string Function(string url)
    {
        this.url = url;
        URLParser parser = new URLParser();
        parser.ParseJsonUrl(url, new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted ));
    }

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
       //Everything you need to be performed once the string is downloaded
    }

      

Finally, in the UrlParser class

public void ParseJsonUrl(string url, DownloadStringCompletedEventHandler handler)
    {
        Uri uri = new Uri(url);
        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += handler;
        webClient.DownloadStringAsync(uri);
    }

      

Hope this helps someone someday!

0


source


Make sure the json file structure you got from WebClient is similar to your parsing structure, for example

{
  "Parameter": {
    "parameter1": "somestring1",
    "parameter2": "somestring2",
    "parameter3": "somestring3"
 }
}



Otherwise, you need to create a class structure similar to the json format you get.

Alternatively, you can create a JObject from json and access the values ​​by key (see here: http://james.newtonking.com/json/help/?topic=html/LINQtoJSON.htm )

+1


source


First of all, you need to see how your parameter class will look like, you can do this through "json to class" applications like here where you insert a link and the application generates how the parameter class should look like, please note that sometimes you need to parse the json manually and also check your internet connection.

+1


source







All Articles