2012-12-13 80 views
1

我正在嘗試爲Windows Phone 8編寫一個庫,並且想知道是否可以使用不使用回調而使用await關鍵字來使用JSON?或者我誤解了這是如何工作的?Windows Phone 8與AWAIT異步JSON

基本上我想要的應用可以說:

string result = Library.Ping(var1, var2); 

庫連接到Web服務和反序列化JSON的動態對象的內容。然後它將其返回到已將請求發送到庫的主應用程序。

回答

6

見的情況下,聯答案的網站宕機:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading.Tasks; 
using System.ComponentModel; 

namespace Win8WinPhone.CodeShare.Extensions 
{ 
    public static class HttpExtensions 
    { 
     public static Task<Stream> GetRequestStreamAsync(this HttpWebRequest request) 
     { 
      var taskComplete = new TaskCompletionSource<Stream>(); 
      request.BeginGetRequestStream(ar => 
      { 
       Stream requestStream = request.EndGetRequestStream(ar); 
       taskComplete.TrySetResult(requestStream); 
      }, request); 
      return taskComplete.Task; 
     } 

     public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request) 
     { 
      var taskComplete = new TaskCompletionSource<HttpWebResponse>(); 
      request.BeginGetResponse(asyncResponse => 
      { 
       try 
       { 
        HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState; 
        HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse); 
        taskComplete.TrySetResult(someResponse); 
       } 
       catch (WebException webExc) 
       { 
        HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response; 
        taskComplete.TrySetResult(failedResponse); 
       } 
      }, request); 
      return taskComplete.Task; 
     } 
    } 

    public static class HttpMethod 
    { 
     public static string Head { get{return "HEAD";} } 
     public static string Post { get{return "POST";} } 
     public static string Put { get{return "PUT";} } 
     public static string Get { get{return "GET";} } 
     public static string Delete { get{return "DELETE";} } 
     public static string Trace { get{return "TRACE";} } 
     public static string Options { get{return "OPTIONS";} } 
     public static string Connect { get{return "CONNECT";} } 
     public static string Patch { get{return "PATCH";} } 
    } 

} 

然後,你可以寫你的函數爲:

public async Task<string> GetMyData(string urlToCall) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToCall); 
    request.Method = HttpMethod.Get; 
    HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); 
    using (var sr = new StreamReader(response.GetResponseStream())) 
    { 
     return sr.ReadToEnd(); 
    } 
} 

而且然後稱之爲:

Tweet myTweet = await GetTweet(tweetID); 

所有功勞分到: @robertftw誰鏈接到:http://www.windowsphonegeek.com/news/windows-8---windows-phone-code-sharing-httpwebrequest-getresponseasync

+0

您也可以立即安裝預發行版便攜式HttpClient程序包。然後你可以使用本地的異步友好的HttpClient。 – kamranicus

+0

@subkamran - 可下載的鏈接? –

+0

@Robert Oschler https://www.nuget.org/packages/Microsoft.Net.Http –

3

你可以非常接近。你最終會得到的是:

string result = await Library.PingAsync(var1, var2); 

首先,安裝Microsoft.Bcl.Async。你Library.PingAsync會有這樣的結構:

public static async Task<string> PingAsync(MyType1 var1, MyType2 var2) 
{ 
    var client = new WebClient(); 
    var stringResult = client.DownloadStringTaskAsync(..); 
    return JSON.Parse(stringResult).Whatever; 
} 
+0

據我所知,Windows Phone 8不支持HttpClient。任何其他想法? –

+3

WebClient或者HttpWebRequest。 http://www.windowsphonegeek.com/news/windows-8---windows-phone-code-sharing-httpwebrequest-getresponseasync – robertk

+0

查看更新的答案。 –