'daki veriler nasıl işlenir Yeni bir Windows Phone 8 uygulaması üzerinde çalışıyorum. Geçerli json verilerini döndüren bir web servisine bağlanıyorum. Verileri görüntülemek için longlistselector kullanıyorum. GetAccountList() içinde dize json kullanırken bu iyi çalışır. ancak DataServices sınıfından veri alırken hata alıyorum "Implicitly 'System.Threading.Tasks.Task'to string" türünü dönüştüremiyor. Neyin yanlış gittiğini bilmiyorum. Herhangi bir yardım edilir. Teşekkürler!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;
}
GÜNCELLEME: Ben küçük bir değişiklik yapmış ve şimdi bir cazibe gibi çalışır. Yardım ettiğin için teşekkür ederim! 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;
}
Yine aynı. "String json = DataService" hatası verir.GetRequest (url); "GetAccountList() içinde – marlonlaan
Evet, burada hata verir, uyumsuzluğun sebebi. Ama lütfen GetReuquest yöntemine bir kesme noktası girin ve 'Gerçekten' istisnasının nerede olduğunu söyleyin :) – Krekkon
Derlenemiyor breakpoint ile http://cuse.nl/Naamloos.jpg – marlonlaan