From 2f1247072d4ea3390d66d677d3900dd45a4a1730 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Date: Tue, 25 Feb 2014 08:50:45 +0100 Subject: [PATCH] No time for comments. --- .../RemoteAgents/RemoteAgents/MainPage.xaml.cs | 14 ++++++++++--- .../RemoteAgents/Model/CallRemoteProcedure.cs | 24 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs b/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs index 8705bb9..032bc50 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/MainPage.xaml.cs @@ -30,10 +30,18 @@ namespace RemoteAgents async private void ButtonRetrieveRemoteData_Click(object sender, RoutedEventArgs e) { - string currentDate = await view.getCurrentDate(); - if (currentDate != null) + try { - this.CurrentDateTextBox.Text = currentDate; + string currentDate = await view.getCurrentDate(); + if (currentDate != null) + { + this.CurrentDateTextBox.Text = currentDate; + } + } + catch (Exception exception) + { + //TODO: logger for Windows Phone 8 :( + Console.WriteLine("ButtonGetDateClicked. Message: {0} Stacktrace: {1}", exception.Message, exception.StackTrace); } } diff --git a/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs b/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs index 1179d86..d00a3ce 100644 --- a/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs +++ b/WindowsPhone/RemoteAgents/RemoteAgents/Model/CallRemoteProcedure.cs @@ -1,4 +1,6 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; using System.Net; using System.Net.Http; using System.Text; @@ -15,8 +17,17 @@ namespace RemoteAgents.WindowsPhone.Model postData.id = "2114567586433855105"; postData.jsonrpc = "2.0"; postData.method = method; + var jsonSettings = new JsonSerializerSettings + { + Error = delegate(object sender, ErrorEventArgs args) + { + //TODO: logger for Windows Phone 8 :( + Console.WriteLine(args.ErrorContext.Error.Message); + args.ErrorContext.Handled = true; + } + }; - string data = JsonConvert.SerializeObject(postData); + string data = JsonConvert.SerializeObject(postData, jsonSettings); HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json-rpc"); HttpResponseMessage response = await this.doCall(uri, content); @@ -27,7 +38,7 @@ namespace RemoteAgents.WindowsPhone.Model { Task responseBytes = response.Content.ReadAsByteArrayAsync(); string responseString = Encoding.UTF8.GetString(responseBytes.Result, 0, responseBytes.Result.Length); - POSTResult postResult = JsonConvert.DeserializeObject>(responseString); + POSTResult postResult = JsonConvert.DeserializeObject>(responseString, jsonSettings); result = postResult.result; } @@ -35,9 +46,16 @@ namespace RemoteAgents.WindowsPhone.Model } + /// + /// Send a POST request to the specified Uri as an asynchronous operation. + /// + /// The Uri the request is sent to. + /// The HTTP request content sent to the server. + /// When some error. + /// System.Threading.Tasks.Task]]>.The task object representing the asynchronous operation. async private Task doCall(string uri, HttpContent content) { - using (HttpClient client = new HttpClient()) + using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) { return await client.PostAsync(uri, content); } -- 2.1.4