2013-10-19 67 views
5

我正在開發一個新的Windows Phone 8應用程序。我正在連接到返回有效json數據的web服務。我正在使用longlistselector來顯示數據。當我在GetAccountList()中使用字符串json時,這工作正常。但是當從DataServices類接收數據時,我收到錯誤「無法隱式地將類型'System.Threading.Tasks.Task'轉換爲字符串」。不知道出了什麼問題。歡迎任何幫助。謝謝!如何處理來自httpclient的數據

DataServices.cs

public async static Task<string> GetRequest(string url) 
    { 
     HttpClient httpClient = new HttpClient(); 

     await Task.Delay(250); 

     HttpResponseMessage response = await httpClient.GetAsync(url); 
     response.EnsureSuccessStatusCode(); 
     string responseBody = await response.Content.ReadAsStringAsync(); 
     Debug.WriteLine(responseBody); 
     return await Task.Run(() => responseBody); 
    } 

AccountViewModel.cs

public static List<AccountModel> GetAccountList() 
    { 
     string json = DataService.GetRequest(url); 
     //string json = @"{'accounts': [{'id': 1,'created': '2013-10-03T16:17:13+0200','name': 'account1 - test'},{'id': 2,'created': '2013-10-03T16:18:08+0200','name': 'account2'},{'id': 3,'created': '2013-10-04T13:23:23+0200','name': 'account3'}]}"; 
     List<AccountModel> accountList = new List<AccountModel>(); 

     var deserialized = JsonConvert.DeserializeObject<IDictionary<string, JArray>>(json); 

     JArray recordList = deserialized["accounts"]; 


     foreach (JObject record in recordList) 
     { 
      accountList.Add(new AccountModel(record["name"].ToString(), record["id"].ToString())); 
     } 

     return accountList; 
    } 

更新:我稍微改變了它,現在就像一個魅力。謝謝你的幫助! DataServices.cs

 //GET REQUEST 
    public async static Task<string> GetAsync(string url) 
    { 
     var httpClient = new HttpClient(); 

     var response = await httpClient.GetAsync(url); 

     string content = await response.Content.ReadAsStringAsync(); 

     return content; 
    } 

AccountViewModel.cs

public async void LoadData() 
    { 
     this.Json = await DataService.GetAsync(url); 
     this.Accounts = GetAccounts(Json); 
     this.AccountList = GetAccountList(Accounts); 
     this.IsDataLoaded = true; 
    } 

    public static IList<AccountModel> GetAccounts(string json) 
    { 
     dynamic context = JObject.Parse(json); 

     JArray deserialized = (JArray)JsonConvert.DeserializeObject(context.results.ToString()); 

     IList<AccountModel> accounts = deserialized.ToObject<IList<AccountModel>>(); 

     return accounts; 
    } 

    public static List<AlphaKeyGroup<AccountModel>> GetAccountList(IList<AccountModel> Accounts) 
    { 
     List<AlphaKeyGroup<AccountModel>> accountList = AlphaKeyGroup<AccountModel>.CreateGroups(Accounts, 
       System.Threading.Thread.CurrentThread.CurrentUICulture, 
       (AccountModel s) => { return s.Name; }, true); 

     return accountList; 
    } 

回答

1

這條線是你的問題:

return await Task.Run(() => responseBody); 

你嘗試了嗎? :

return responseBody; 

試試這個太:

public async static List<AccountModel> GetAccountList() 
{ 
    string json = await DataService.GetRequest(url); 
    ... 
} 
+0

仍然一樣。它給出了「string json = DataService」的錯誤。GetRequest(url);「GetAccountList() – marlonlaan

+0

是的,它會給出錯誤,導致異步。但是請在GetReuquest方法中取一個斷點並告訴」真正「異常在哪裏出現:) – Krekkon

+0

無法編譯與斷點。http://cuse.nl/Naamloos.jpg – marlonlaan

0

有幾件事情在這裏。首先,錯誤

無法隱式轉換類型「System.Threading.Tasks.Task」字符串 此錯誤是從呼叫來DataService.GetRequest(url)。此方法確實會返回一個字符串。 Tt返回Task,其中T是一個字符串。有很多方法可以使用此方法的結果。第一個(也是最好的/最新的)是等待該方法的調用。

string json = await DataService.GetResult(url); 

此更改需要您將async鍵盤添加到您的方法

public async static List<AccountModel> GetAccountList() 

這是新的異步/等待模式。添加這些字會告訴編譯器cal方法是異步的。它允許你進行異步調用,但是編寫代碼就好像它是同步的。 調用該方法的其他方法是直接使用Task對象。

// First is to use the Result property of the Task 
// This is not recommended as it makes the call synchronous, and ties up the UI thread 
string json = DataService.GetResult(url).Result; 

// Next is to continue work after the Task completes. 
DataService.GetResult(url).ContinueWith(t => 
{ 
    string json = t.Result; 
    // other code here. 
}; 

現在爲GetResult方法。使用異步/等待模式需要您從方法返回任務。即使返回類型爲任務,你的代碼應該返回T.因此,作爲Krekkon提到的,你應該回線改爲

return responseBody; 

這裏是一個great article有關從異步方法返回任務。