获取JSON字符串URL从在Windows Phone 8的 [英] Getting JSON string from URL in Windows Phone 8

查看:136
本文介绍了获取JSON字符串URL从在Windows Phone 8的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让从URL中的Windows Phone 8应用JSON字符串。
我只需要调用返回此字符串callbackurl,这是相当多了,但不知何故,我一直停留在这几天,我只是不明白怎么做了。

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

我有一个包含2种方法这是一个urlparser类:

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

有关现在我只是想显示包含在其中的一个参数我JSON字符串,当然我的方法将执行其他的事情,一旦我得到这个工作。

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

我看起来一个名为参数在我urlparser.cs文件的开头类像

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

'System.Reflection.TargetInvocationException'

我跟着这个教程的 http://blogs.msdn.com/b/pakistan/archive/2013/06/23/10425845。 ASPX
和看到别的那些是几乎同样的事情的负荷,但不幸的是,这是行不通的。在一些教程,他们用DownloadString而不是DownloadStringAsync但我不能调用此方法(可能不可用WP8),并在他们使用一些其他的教程方法等待,但我不明白我在哪里应该把等待的声明,我应该补充什么其他代码段

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

此外,一旦我能够得到我的变种我的JSON数据,如果有人能告诉我如何从另一个类访问它,那就太好了!

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 !

谢谢!

推荐答案

所以,搞清楚问题是什么之后,我想我应该回答的问题,如果有人有同样的问题一天。
的事情是,我在我的ParseJsonUrl功能的一些代码行后

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



这是问题。此外,ParseJsonUrl被认为是由一个按钮调用的函数调用,并且调用后,该函数还执行其他呼叫,并且按钮了。

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.

和所有thoose呼叫导致一个事实,即字符串从未下载,直到执行该是由按钮调用的一切,我需要之前,该字符串

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.

的解决方案是创建在调用我的UrlParser.ParseJsonUrl功能,通过该事件在其参数的函数类DownloadStringCompleted方法。

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.

然后,由执行需要的一切按一下按钮被设置在DownloadStringCompleted方法

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
    }

在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
    }

和finaly在UrlParser类

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 !

这篇关于获取JSON字符串URL从在Windows Phone 8的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆