我正在尝试创建一个后台代理来定期更新 Windows Phone 上用户的动态磁贴。
目前,我的代理代码是:
string where = "";
private GeoCoordinate MyCoordinate = null;
HttpWebResponse webResponse;
...
protected override void OnInvoke(ScheduledTask task)
{
System.Diagnostics.Debug.WriteLine("Invoked");
findMe();
NotifyComplete();
}
private void ResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult);
MemoryStream tempStream = new MemoryStream();
webResponse.GetResponseStream().CopyTo(tempStream);
}
private async void findMe()
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
try
{
Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10));
MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
// var uri = new Uri("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1");
// var client = new HttpClient();
var webRequest = (HttpWebRequest)HttpWebRequest.CreateHttp("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1");
webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), webRequest);
System.Diagnostics.Debug.WriteLine("findMe after response");
System.Diagnostics.Debug.WriteLine(MyCoordinate.Latitude);
System.Diagnostics.Debug.WriteLine(MyCoordinate.Longitude);
// var response = await client.GetStringAsync(uri);
System.Diagnostics.Debug.WriteLine(webResponse.ToString());
JToken token = JArray.Parse(webResponse.ToString())[0];
// JToken token = JArray.Parse(response)[0];
var name = token.Next.First.First;
var address = token.Next.Last.First;
where = name + ", " + address;
}
catch (Exception)
{
System.Diagnostics.Debug.WriteLine("findMe died");
where = "";
}
System.Diagnostics.Debug.WriteLine("findMe complete");
UpdateAppTile();
}
private void UpdateAppTile()
{
System.Diagnostics.Debug.WriteLine("UpdateAppTile");
ShellTile appTile = ShellTile.ActiveTiles.First();
if (appTile != null)
{
StandardTileData tileData = new StandardTileData
{
BackContent = where
};
appTile.Update(tileData);
}
System.Diagnostics.Debug.WriteLine("Update Completed: " + where);
}
当我尝试运行它时,代码到达webRequest.BeginGetResponse
并随后停止。下一行,并ResponseCallback
没有到达。
我的代码的旧版本被注释掉了,我认为这是问题,但它也遇到了同样的问题。