Getting JSON string from URL in Windows Phone 8

情到浓时终转凉″ 提交于 2019-12-10 20:26:42

问题


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 !


回答1:


Make sure the structure of the json file received from WebClient is similar to your parsed class structure i.e.

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

Otherwise you need to create a class structure similar to the format of json you're receiving.

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




回答2:


First of all you you need to see how your parameter class would be like , you can do through "json to class" apps like here , where you insert the link and the app generates how the parameter class should be like , please note that sometimes you have to parse the json manually , and also please check your internet connection .




回答3:


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

webClient.DownloadStringAsync(uri);

And that was the problem. Also, the ParseJsonUrl is called by a function that's called by a button, and after that call, the function also performs other calls, and the button too.

And all thoose calls lead to the fact that the string is never downloaded until EVERYTHING that's called by the button is performed, and I needed the string before that.

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

Then, everything that needs to be performed by the button click is set in that DownloadStringCompleted method

What it looks like in code :

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
    }

And finaly 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 !



来源:https://stackoverflow.com/questions/25203377/getting-json-string-from-url-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!