Windows phone 7 How to create events to update UI - silverlight

Hi I'm making an app that when it launches makes a HttpWebRequest, receives some XML and puts it in a list. This code is in the Application_Launching method in App.xaml.cs . This list is then used in a listpicker on the first page of the app.
However because HttpWebRequest executes on a different thread the list is not populated when I assign it to to the Listpickers itemSource.
I've been told I should have an event that fires after the list is full and a listener on my first page to populate the list when this happens. How would I declare this event and its listener?

You can use HttpWebRequest and make AsyncCallback or use WebClient class which has an event DownloadStringCompleted. An example.

public void GetXMLfromServer()
{
try
{
string url = "";//your url here
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.BeginGetResponse(new AsyncCallback(GetXMLfromServerCompleted),
request);
}
catch (Exception ex)
{
}
}
private void GetXMLfromServerCompleted(IAsyncResult asynchronousResult)
{
try
{
string resultString = "";
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
resultString = streamReader1.ReadToEnd();
}
//**Put your code here to populate the list**
}
catch (Exception ex)
{
}
}

Related

Application dispatcher invoke freezes the application

I am having problem with the application freezing. Let me explain my scenario, I have a service which does an async call to a database to get a list of items, It is run by a task. Inside this task I have a try catch block, so it looks like this
public Task<List<T>> ComboListAsync(int? id = null, EnumDTO dto = EnumDTO.Default)
{
return Task.Run(() =>
{
using (var context = new ContextService())
{
try
{
return GetComboList(id, dto, context);
}
catch (Exception e)
{
Handler.DatabaseConnectionException();
throw;
}
}
});
}
Then it throws an exception as GetComboList its just this (for the moment)
protected virtual List<T> GetComboList(int? id, EnumDTO dto, ContextService context)
{
throw new NotImplementedException();
}
So the call catches the exception and goes inside here
public void Show(string message)
{
Message = message;
Application.Current.Dispatcher.Invoke(() =>
{
dialogView = new DialogView() {DataContext = this, Owner = Application.Current.MainWindow};
dialogView.ShowDialog();
});
}
Now the Dispatcher freezes the app, I tried to change it to use begin invoke, it does the same. Without the dispatcher I get an error message that the calling thread is not a STA. I simply want to display my message in a dialog window, that there was a problem connecting to a database. Can anyone help?
I looked online and there is many threads about dispatcher, but none actually show a solution that will fix my issue.
Thank you
EDIT
Code which calls the ComboListAsync
protected override void RetrieveRelatedActiveLists()
{
MyCollection = service.ComboListAsync().Result;
}
Its a deadlock because of the calling code is using the .Result.
Using service.ComboListAsync().Result makes the UI thread await for this method to return, when you call Application.Current.Dispatcher.Invoke from within it you are sending a message to the UI thread that is awaiting the return of method itself.
You must await the method service.ComboListAsync() like this:
protected override async void RetrieveRelatedActiveLists()
{
MyCollection = await service.ComboListAsync();
}

CodeNameOne InfiniteProgress Issue

I am having issues with rendering next screen if Infinite Progress is used and if this piece of code is uncommented then I am able to show the next screen where it displays a list.
final Form poList = (Form) super.createContainer(getResourceFilePath(), "POList");
ConnectionRequest request = new ConnectionRequest()
{
Hashtable response = null;
protected void readResponse(InputStream input)
{
//Read and parse the response
}
protected void postResponse()
{
if (response != null)
{
try
{
//Get a sorted List from the response and use it to fill the list
poList.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};
request.setUrl(poListUrl);
request.setPost(false);
request.addRequestHeader("Authorization","Bearer "+accessToken);
request.setContentType("text/xml");
/*
If these three lines are commented then the next form is shown properly
*/
InfiniteProgress ip = new InfiniteProgress();
Dialog dlg = ip.showInifiniteBlocking();
request.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(request);
You have a race condition between disposing the infinite progress and the showing of the next form.
Move the dialog showing code before
ConnectionRequest request = new ConnectionRequest()
Then
dlg.dispose();
//Get a sorted List from the response and use it to fill the list
poList.show();

HttpWebRequest "POST" returning server error (Exception)

I'm trying to call the google URL shortner API from a WP7 app, I tried first on a Console Application by doing this:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"longUrl\":\"http://www.google.com/\"}";
Console.WriteLine(json);
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Console.WriteLine(responseText);
}
Console.Read();
and it worked fine, and returned everything OK, but when I try to do it on a Windows Phone App like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
testConnection();
}
private void testConnection()
{
if (!NetworkInterface.GetIsNetworkAvailable())
MessageBox.Show("There's no internet connection, please reconnect to the internet and try again");
else
{
var req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
req.ContentType = "application/json";
req.Method = "POST";
req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req);
textBlock2.Text = "Done";
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data:
string postData = "http://www.google.com";
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
textBlock1.Text= streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
}
}
The code always goes to the catch of the try method and gives "not found" error.
There is a known issue with SSL and not-trusted certificates. Connection to a web site with SSL that require ClientCertificates is not supported in the current Windows Phone 7 application model (Only BasicAuthentication is supported). You can read about the same problem here: http://forums.create.msdn.com/forums/p/65076/398730.aspx
I had a similar issue (which makes sense since I started out with this code as my example...) and when I changed the following code it started working to the point where I could progress forward again.
... from:
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
... to (the equivalent of):
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

HttpWebRequest.EndGetResponse throws a NotSupportedException in Windows Phone 7

in a Silverlight-Windows Phone 7-project I am creating an HttpWebRequest, get the RequestStream, write something into the Stream and try to get the response, but I always get a NotSupportedException:
"System.Net.Browser.OHWRAsyncResult.AsyncWaitHandle threw an exception of type 'System.NotSupportedException'
My production code is far more complicated, but I can narrow it down to this small piece of code:
public class HttpUploadHelper
{
private HttpWebRequest request;
private RequestState state = new RequestState();
public HttpUploadHelper(string url)
{
this.request = WebRequest.Create(url) as HttpWebRequest;
state.Request = request;
}
public void Execute()
{
request.Method = "POST";
this.request.BeginGetRequestStream(
new AsyncCallback(BeginRequest), state);
}
private void BeginRequest(IAsyncResult ar)
{
Stream stream = state.Request.EndGetRequestStream(ar);
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
}
private void BeginResponse(IAsyncResult ar)
{
// BOOM: NotSupportedException was unhandled;
// {System.Net.Browser.OHWRAsyncResult}
// AsyncWaitHandle = 'ar.AsyncWaitHandle' threw an
// exception of type 'System.NotSupportedException'
HttpWebResponse response = state.Request.EndGetResponse(ar) as HttpWebResponse;
Debug.WriteLine(response.StatusCode);
}
}
public class RequestState
{
public WebRequest Request;
}
}
Does anybody know what is wrong with this code?
The NotSupportedException can be thrown when the request stream isn't closed before the call to EndGetResponse. The WebRequest stream is still open and sending data to the server when you're attempting to get the response. Since stream implements the IDisposable interface, a simple solution is to wrap your code using the request stream in a using block:
private void BeginRequest(IAsyncResult ar)
{
using (Stream stream = request.EndGetRequestStream(ar))
{
//write to stream in here.
}
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
}
The using block will ensure that the stream is closed before you attempt to get the response from the web server.
The issue is with how you're dealing with the accessing the original requests in the callback from BeginGetResponse.
Rather than holding a reference ot the state, get a reference back to the original request with:
var request = (HttpWebRequest)asynchronousResult.AsyncState;
Have a look at this very basic (but working) example of implementing logging in by posting email and password credentials to a website.
public static string Email;
public static string Password;
private void LoginClick(object sender, RoutedEventArgs e)
{
Email = enteredEmailAddress.Text.Trim().ToLower();
Password = enteredPassword.Password;
var request = (HttpWebRequest)WebRequest.Create(App.Config.ServerUris.Login);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetRequestStream(ReadCallback, request);
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
var request = (HttpWebRequest)asynchronousResult.AsyncState;
using (var postStream = request.EndGetRequestStream(asynchronousResult))
{
using (var memStream = new MemoryStream())
{
var content = string.Format("Password={0}&Email={1}",
HttpUtility.UrlEncode(Password),
HttpUtility.UrlEncode(Email));
var bytes = System.Text.Encoding.UTF8.GetBytes(content);
memStream.Write(bytes, 0, bytes.Length);
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
postStream.Write(tempBuffer, 0, tempBuffer.Length);
}
}
request.BeginGetResponse(ResponseCallback, request);
}
private void ResponseCallback(IAsyncResult asynchronousResult)
{
var request = (HttpWebRequest)asynchronousResult.AsyncState;
using (var resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
using (var streamResponse = resp.GetResponseStream())
{
using (var streamRead = new StreamReader(streamResponse))
{
string responseString = streamRead.ReadToEnd();
// do something with responseString to check if login was successful
}
}
}
}
NotSupportedException can also be thrown when string url is too long. I had messed up and attached post data to url instead of post body. When the data was short, it worked just fine, but once it grew too large - EndGetResponse crashed.
Change this:
state.Request.BeginGetResponse(
new AsyncCallback(BeginResponse), state);
To this:
state.Request.BeginGetResponse(BeginResponse, state);

WCF Data Services UpdateObject not working

I have a Silverlight client with a grid getting data from WCF Data Service. Works fine.
However if I want to update some changed grid row, the service data context UpdateObject is not working:
DataServiceContext.UpdateObject(MyGrid.SelectedItem);
foreach (Object item in DataServiceContext.Entities)
{
//
}
DataServiceContext.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, DataServiceContext);
I just have created a loop to inspect the values for the entities items and the value is not updated at all. BeginSaveChanges works fine, but it just uses not updated values.
Any ideas how to fix that?
thanks
Right a fully flushed out SaveChanges that will show the error message if EndSaveChanges() fails, like the code sample below. Obviously you can't use the console to write out your message in silverlight, but you get the idea.
For instance, when I wrote the following sample, I found that I was getting a forbidden error, because my entity set had EntitySetRights.AllRead, not EntitySetRights.All
class Program
{
private static AdventureWorksEntities svc;
static void Main(string[] args)
{
svc =
new AdventureWorksEntities(
new Uri("http://localhost:5068/AWDataService.svc",
UriKind.Absolute));
var productQuery = from p in svc.Products
where p.ProductID == 740
select p;
var product = productQuery.First();
ShowProduct(product);
product.Color = product.Color == "Silver" ? "Gray" : "Silver";
svc.UpdateObject(product);
svc.BeginSaveChanges(SaveChangesOptions.Batch, OnSave, svc);
ShowProduct(product);
Console.ReadKey();
}
private static void ShowProduct(Product product)
{
Console.WriteLine("Id: {0} Name: {1} Color: {2}",
product.ProductID, product.Name, product.Color);
}
private static void OnSave(IAsyncResult ar)
{
svc = ar.AsyncState as AdventureWorksEntities;
try
{
WriteResponse(svc.EndSaveChanges(ar));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void WriteResponse(DataServiceResponse response)
{
if(response.IsBatchResponse)
{
Console.WriteLine("Batch Response Code: {0}", response.BatchStatusCode);
}
foreach (ChangeOperationResponse change in response)
{
Console.WriteLine("Change code: {0}", change.StatusCode);
if(change.Error != null)
{
Console.WriteLine("\tError: {0}", change.Error.Message);
}
}
}
}

Resources