我正在努力发展我们的划船社区使用的应用程序。它的一部分是一个网站上的地图,其中有最近的船只位置。到目前为止,一切都正常,但我想添加一个连续的背景跟踪作为应用程序的功能。
应用程序工作了一段时间,但由于超时导致上传失败。这些都不是来自API的限制,在我看来,应用程序就是不能再访问网络了。
我所有的位置信息都依赖于https://github.com/jamesmontemagno/GeolocatorPlugin。该插件还在安卓系统上使用了https://github.com/jamesmontemagno/CurrentActivityPlugin/。
所以,这是我在收到手机的位置更新后所做的事情。
事件处理程序设置:
[...]
TimeSpan span = new TimeSpan(0, 20, 0); // Prod: 0, 30, 0
double distance = 500; // Prod: 2700
bool includeHeading = false; //don't alert on heading changes
ListenerSettings listenerSettings = new ListenerSettings
{
AllowBackgroundUpdates = true,
DeferralDistanceMeters = distance, // adjustable in app 2-10 nautical Miles
DeferLocationUpdates = true,
DeferralTime = span, // adjustable in app 15-120 Minutes
ListenForSignificantChanges = false,
PauseLocationUpdatesAutomatically = false,
ActivityType = ActivityType.OtherNavigation
};
var success = await CrossGeolocator.Current.StartListeningAsync(span,distance,includeHeading,listenerSettings);
if (success)
{
CrossGeolocator.Current.PositionChanged += async (s, e) =>
{
Console.WriteLine("\n\n -_-_-_-_-_ Position Event -_-_-_-_-_-_ \n\n");
Trackpoint trackpoint = new Trackpoint(e.Position);
Trackpoint point = await Tracking(trackpoint);
};
}
[...]事件处理程序调用:(在应用程序的开发路线图下还会有更多的事情发生)
public async Task<Trackpoint> Tracking(Trackpoint trackpoint = null)
{
return await tracker.LogPoint(trackpoint);
}在tracker.LogPoint(trackpoint)中,来自Hevent处理程序的坐标被添加到一个列表中(该列表应该是空的,但可以保存以前的跟踪点,以防上传失败)。
public async Task<Trackpoint> LogPoint(Trackpoint location = null)
{
if (location == null)
{
location = await GetCoordinates(); // Call GPS if no Trackpoint is given when Tracking is done fully manual
}
if (location != null)
{
Console.WriteLine("Trackpoint Queue was: {0}", track.Count);
track.Add(location); // Add current location to end of the list (defined as private in the class)
int trackpointQueue = track.Count;
int uploadedTrackpoints = 0;
Console.WriteLine("Trackpoint Queue now: {0}", trackpointQueue);
foreach (var position in track)
{
Console.WriteLine("Uploading Trackpoint: {0} | {1} | {2}", position.Timestamp, position.Latitude, position.Longitude);
if (App.api.UploadTrackPoint(track[track.Count - 1])) // upload first trackpoint in List (ideally current) and remove it from List, repeat with more items in queue.
{
uploadedTrackpoints++;
}
else
{
Console.WriteLine("Error uploading Trackpoint trying again next time");
// Because uploading attempt failed, we don't want to try again now leave the loop until next call.
break;
}
}
track.RemoveRange(0, uploadedTrackpoints); // remove all uploaded Trackpoints from Queue
}
return location;
}上载以multipart/formdata的形式上传到现有的API。在4-10个事件之后,所有进一步的上传都会失败,因为发生了网络超时而引发异常。将应用程序带回前台几乎变得没有响应。
public bool UploadTrackPoint(Trackpoint location)
{
bool success = false;
if (CanUseData())
{
string posturl = settings.BlogURL + "(THE API URL)" + "(THE API KEY)";
string agent = "User-Agent: (MY APP NAME)/" + settings.Version + " (" + DeviceInfo.Platform + ", " + DeviceInfo.VersionString + ")";
Console.WriteLine("Uploading: {0} ", Trackpoint.ConvertToString(location));
try
{
var response = MessageUpload.MultipartFormDataPost(false, posturl, agent, location.Pairs);
Console.WriteLine("HTTP response StatusCode: " + response.StatusCode + "\n Content: " + response.ResponseUri);
if (response.StatusCode == System.Net.HttpStatusCode.OK) success = true;
}
catch (Exception ex)
{
Console.WriteLine("FAILED!!! \n\n({0})\n\n", ex);
}
}
return success;
}在iOS (physical iPhone 6)和iOS模拟器以及安卓模拟器上也会发生完全相同的行为。然而,我读了很多关于iOS和最新的安卓API的背景限制,但我怀疑这是这里的问题,因为它在两个平台上都是一致的。因此,我假设我在这里做错了什么,在撞墙之前填充了一些排序缓冲区。然而,该应用程序不会被任何操作系统终止,而在上传开始失败并且积压不断增加后,我可以在控制台上看到一些-_-_-_-_-_ Position Event -_-_-_-_-_-_。最终,在一些超时发生后,甚至位置更新也会停止。
您将从哪里开始进一步的调试?
发布于 2019-08-21 15:34:38
以防有人偶然发现这个。除了@SushiHangover提到的缺少后台服务之外,更大的问题是对WebResponse的错误处理。
事实证明,我的直觉感觉就是在这里遇到了某种障碍:在我的Http调用完成后,应用程序只读取头文件,从不处理响应对象,应用程序保持所有连接都是打开的。在exact 10 (在iOS上)之后,应用程序无法打开新的连接,从而导致超时。
https://stackoverflow.com/questions/57127063
复制相似问题