使用REST服务 [英] Consume REST services

查看:91
本文介绍了使用REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好, 

我试图从Twitter获取一些数据。 

I am trying to feth some data from Twitter . 

通常我使用Hammock,但因为它不适用于Windows 8应用程序(我无法在我的应用程序上安装此软件包) 

Usually I use Hammock but as it is not available for Windows 8 apps  (I am not able to install this package on my app)  

这就是我通常使用Hammock来获取Twitter数据的方式(在这个例子中我发布了一个状态):

This is how I usually use Hammock  to fetch Twitter Data  (In this example I post a status) :

var credentials = new OAuthCredentials
            {
                Type = OAuthType.ProtectedResource,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = AppSettings.consumerKey,
                ConsumerSecret = AppSettings.consumerKeySecret,
                Token = this.accessToken,
                TokenSecret = this.accessTokenSecret,
                Version = "1.0"
            };



 var restClient = new RestClient
                {
                    Authority = "https://api.twitter.com",
                    HasElevatedPermissions = true,
                    Credentials = credentials

                };


                var restRequest = new RestRequest
                {
                    Credentials = credentials,
                    Path = "1.1/statuses/update_with_media.json",
                    Method = WebMethod.Post


                };



                restRequest.AddField("status", "Test");











                restClient.BeginRequest(restRequest,RequestCallBack);






        private void AutreRequestCallBack(RestRequest request, RestResponse response, object obj)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (reponse.StatusCode == HttpStatusCode.OK)
                {


                   MessaBox.Show("Success");


                }




                else
                {
                    MessageBox.Show("Failure");


                }
            }
                );



        }







我如何使用类似的方法来获取(或发布)  Windows 8应用程序中的Twitter数据? 

How can I use a similar method to  get (or post)   Twitter Data in a Windows 8 Application ? 

请告诉我是否可以使用吊床,但我误导了

Please tell me if I can use Hammock but I was mislead

谢谢!

推荐答案

吊床不能在windows 商店app中使用。您可以从Twitter服务器下载json字符串,然后解析json字符串。请参考以下链接:

Hammock cannot used in windows store app. You can download the json string from the Twitter server and then parse the json string. Please refer to the link below:

http://www.codeproject.com/Articles/546430/Parse-JSON-to-Csharp-in-WinRT-Silverlight-WPF-Wind

在链接中,您可以使用以下方法加载Twitter数据:

In the link, you can use the method below to load Twitter data:

public static async Task<ObservableCollection<TwitterItem>> Load(string uri)
{
    ObservableCollection<TwitterItem> result = new ObservableCollection<TwitterItem>();
    try
    {
        var client = WebRequest.CreateHttp(uri);
        var response = await client.GetResponseAsync();

        #region ** parse twitter data
        using (System.IO.Stream stream = response.GetResponseStream())
        {
            // parse Json data
            DataContractJsonSerializer rootSer = new DataContractJsonSerializer(typeof(RootObject));

            RootObject root = (RootObject)rootSer.ReadObject(stream);
            foreach (Result res in root.results)
            {
                TwitterItem ti = new TwitterItem();
                ti.Title = res.text;
                DateTimeOffset dateOffset;
                if (DateTimeOffset.TryParse(res.created_at, out dateOffset))
                {
                    ti.PublishDate = dateOffset.UtcDateTime;
                }
                ti.Handle = res.from_user;
                if (res.entities.urls != null && res.entities.urls.Count > 0)
                {
                    ti.Link = res.entities.urls[0].url;
                }
                result.Insert(GetFirstIndexOfOlderTwitterItem(ti.PublishDate, result), ti);
            }
        }
        #endregion
    }
    catch
    {
    }
    return result;
}


祝福!

Best Wishes!


这篇关于使用REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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