Xamarin.Android crashes without Internet - android

I create app on Xamarin.Android. There is a socket, with it's help I'm getting some data for my app. I'm checking the internet connection like this:
_timer = new Timer(CheckNetworkAvailable, new AutoResetEvent(false), 0, 10000);
It calls in OnCreate method.
cts = new CancellationTokenSource();
var isNetwork = await Task.Run(() => this.NetworkRechableOrNot(), cts.Token);
var linear = SupportActionBar.CustomView.FindViewById<LinearLayout>(Resource.Id.linearForActionBanner);
var identOn = linear.FindViewById<ImageView>(Resource.Id.identificator_on);
var identOff = linear.FindViewById<ImageView>(Resource.Id.identificator_off);
RunOnUiThread(() =>
{
identOn.Visibility = !isNetwork ? ViewStates.Gone : ViewStates.Visible;
identOff.Visibility = !isNetwork ? ViewStates.Visible : ViewStates.Gone;
});
if (isNetwork)
{
if (isNetwork != oldNet)
{
oldNet = isNetwork;
MainDataClass.UpdateSymbolsList();
SocketClass.Start();
}
}
else
{
oldNet = isNetwork;
cts.Cancel();
SocketClass.Stop();
}
Here oldNet - previos status of internet access.
private bool NetworkRechableOrNot()
{
try
{
var connectivityManager = (_Net.ConnectivityManager)GetSystemService(Context.ConnectivityService);
var activeConnection = connectivityManager.ActiveNetworkInfo;
return (activeConnection != null) && activeConnection.IsConnected;
}
catch (Exception)
{
return false;
}
}
And this is the result method for checking.
SocketClass - class for socket and getting data; Start method creating new Task and Start it, socket created via SocketIOClient.Socket; Stop method calls Close method of Socket. And it all works fine until I try to disable internet connection, I got only once NameResolutionFailure. What can be the reason of it? And what should I change for meking it works fine? Thanks.

I think the main reason is your internet is off. You need a internet to try resolve some address.

I solved it, there was not correct realization of socket, it come in loop because when socket gets error it calls itself and doesn't finish that's why it crashes.

Related

httpClient.PostAsync crashes app in Xamarin Android JobService

I have an Android Xamarin app that handles notifications. When a notification is displayed, there are buttons that ask for a response. The app needs to send this response back to a server via an httpClient.PostAsync call. I am using the same http client wrapper in other parts of the code and it is working correctly. However, when I call it from the JobService code, the app crashes. I have enclosed the http call in a try/catch and no exception occurs. There are also no errors in the device log. i would like to know how to debug this. Here is my flow:
I have a class that derives from FirebaseMessagingService with an OnMessageReceived method. That gets called when a notification arrives. I build a local notification via the notification manager and call .Notify. The notification appears with the buttons. I have a BroadcastReceiver with an OnReceive method. That method schedules a job to do the post back of the button click. The job gets started and runs until the point I call the PostAsync. From there it crashes with no exception. Here is the relevant part of the JobWorker:
public override bool OnStartJob(JobParameters jobParams)
{
_backgroundWorker = Task.Run(() => { DoWork(jobParams); });
return true;
}
private void DoWork(JobParameters jobParams)
{
var logger = App.ResolveDependency<ILogger>() as ILogger;
var callActions = App.ResolveDependency<ICallActionsHandler>() as ICallActionsHandler;
var callToken = jobParams.Extras.GetString(JobParameterCallToken);
var subsciberPhoneNumber = jobParams.Extras.GetString(JobParameterSubscriberPhoneNumber);
var action = jobParams.Extras.GetString(JobParametersCallAction);
logger.TraceInfo($"starting {nameof(CallActionService)}: starting job {jobParams.JobId}");
callActions.SendAction(
callToken,
subsciberPhoneNumber,
(CallActions)Enum.Parse(typeof(CallActions), action));
}
The SendAction code calls the http client wrapper. The http client wrapper code looks like this:
public async Task<int> PostAsync(string api, object message)
{
var apiUrl = Constants.DefaultAppApi + api;
var contentText = JsonConvert.SerializeObject(message);
var content = new StringContent(contentText, Encoding.UTF8, "application/json");
var backOff = 10;
var retryCount = 5;
HttpResponseMessage response = null;
for (var attempt = 1; attempt <= retryCount; attempt++)
{
_logger.TraceInfo($"DataServerClient Post message: {message.GetType().Name}, attempt = {attempt}");
try
{
response = await _client.PostAsync(apiUrl, content);
}
catch (Exception ex)
{
if (attempt == retryCount)
_logger.TraceException($"DataServerClient Post failed", ex);
}
if (response != null && response.IsSuccessStatusCode)
{
_logger.TraceInfo($"DataServerClient post was successful at retry count: {attempt}");
break;
}
backOff *= 2;
await Task.Delay(backOff);
}
return (int)response.StatusCode;
}
Can anyone provide clues for why this is failing or how I can gather diagnostics to find out what is happening? As I mentioned, the exception is not caught, the task that I create gets marked as completed, and no message gets posted.

Geolocation.GetLocationAsync is not working

I have a Xamarin.Forms application that supports UWP, iOS, and Android. Specifically, I am now testing my app on Android emulator. For getting location, I am using Xamarin.Essentials. Here are pieces of my code:
In a PageModel:
bu = await GeolocationSrvc.GetBusinessUnitAsync();
And here is the implementation of the method above:
public static async Task<BusinessUnits> GetBusinessUnitAsync()
{
BusinessUnits bu = BusinessUnits.Aus;
try
{
Location location = await GetLocation().ConfigureAwait(false);
IEnumerable<Placemark> placemarks = await Geocoding.GetPlacemarksAsync(location);
Placemark placemark = placemarks?.FirstOrDefault();
string countryCode = placemark?.CountryCode;
switch (countryCode)
{
case "AQ":
case "AU":
case "NZ":
bu = BusinessUnits.Aus;
break;
default:
bu = BusinessUnits.NA;
break;
}
}
catch (Exception)
{
throw;
}
return bu;
}
private static Task<Location> GetLocation()
{
GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
TaskCompletionSource<Location> locationTaskCompletionSource = new TaskCompletionSource<Location>();
Device.BeginInvokeOnMainThread(async () =>
{
locationTaskCompletionSource.SetResult(await Geolocation.GetLocationAsync(request));
});
return locationTaskCompletionSource.Task;
}
When the execution comes to
locationTaskCompletionSource.SetResult(await Geolocation.GetLocationAsync(request));
I am asked if I want to allow the app to get my location. If I press Yes, it works as expected. But if I press No, the location is never returned (not even null), the following code is never executed. I expected in case of answering No to use default value set in
BusinessUnits bu = BusinessUnits.Aus;
But it does not happen.
Alternate approach would be to check for location permission before hand using a simple dependency service for each platform.
If the permission is granted, then continue with location fetch. Else prompt user to get permission.
For ex. Android implementation to check location permission:
public bool IsLocationPermissionGranted()
{
if (ContextCompat.CheckSelfPermission(Application.Context,
Manifest.Permission.AccessFineLocation) == Permission.Granted)
{
return true;
}
return false;
}
You're not setting the Exception of your TaskCompletionSource object:
private static Task<Location> GetLocation()
{
GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
TaskCompletionSource<Location> locationTaskCompletionSource = new TaskCompletionSource<Location>();
Device.BeginInvokeOnMainThread(async () =>
{
try
{
locationTaskCompletionSource.SetResult(await Geolocation.GetLocationAsync(request));
}
catch(Exception exception)
{
locationTaskCompletionSource.SetException(exception);
locationTaskCompletionSource.SetResult(null);
}
});
return locationTaskCompletionSource.Task;
}

Signal R persistent connection on phonegap apps (android and ios)

I am using SignalR persistent connection for smooth communication between my client and server. All the web communications work really fine.
Now I am trying to do the same with my PhoneGap apps. It doesn't work. Is there any reason?
Here is my code
StartEvents = function () {
connection = $.connection(connectionString);
connection.logging = true;
connection.received(function (result) {
for (i = 0; i < result.events.length; ++i) {
}
});
connection.start().done(function () {
console.log("connected");
});
};
It does log out connected at the start when the app is connected the first time. But then as events keep happening nothing is pushed from the server.
Server code is this
public class ApiConnection : PersistentConnection
{
protected override Task OnConnected(IRequest request, string connectionId)
{
StartBroadcast(connectionId);
return base.OnConnected(request, connectionId);
}
}
private void StartBroadcast(string connectionId)
{
var timerCall = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1));
_subscription = timerCall.Subscribe(async res =>
{
List<dynamic> result = pull();
if (result != null && result.Count > 0)
await Connection.Send(connectionId, new
{
id = _newestId,
events = result
});
});
}

System.Net.Http.HttpClient with AutomaticDecompression and GetAsync (timeout) vs GetStringAsync (working

I have the following code to make requests to a REST API, using Xamarin and an Android device:
public class ApiBase
{
HttpClient m_HttpClient;
public ApiBase(string baseAddress, string username, string password)
{
if (!baseAddress.EndsWith("/"))
{
baseAddress += "/";
}
var handler = new HttpClientHandler();
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip;
}
m_HttpClient = new HttpClient(handler);
m_HttpClient.BaseAddress = new Uri(baseAddress);
var credentialsString = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
m_HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsString);
m_HttpClient.Timeout = new TimeSpan(0, 0, 30);
}
protected async Task<XElement> HttpGetAsync(string method)
{
try
{
HttpResponseMessage response = await m_HttpClient.GetAsync(method);
if (response.IsSuccessStatusCode)
{
// the request was successful, parse the returned string as xml and return the XElement
var xml = await response.Content.ReadAsAsync<XElement>();
return xml;
}
// the request was not successful -> return null
else
{
return null;
}
}
// some exception occured -> return null
catch (Exception)
{
return null;
}
}
}
If i have it like this, the first and the second call to HttpGetAsync work perfectly, but from the 3rd on the GetAsyncstalls and eventually throws an exception due to the timeout. I send these calls consecutively, there are not 2 of them running simultaneously since the results of the previous call are needed to decide the next call.
I tried using the app Packet Capture to look at the requests and responses to find out if i'm sending an incorrect request. But it looks like the request which fails in the end is never even sent.
Through experimentation i found out that everything works fine if don't set the AutomaticDecompression.
It also works fine if i change the HttpGetAsync method to this:
protected async Task<XElement> HttpGetAsync(string method)
{
try
{
// send the request
var response = await m_HttpClient.GetStringAsync(method);
if (string.IsNullOrEmpty(response))
{
return null;
}
var xml = XElement.Parse(response);
return xml;
}
// some exception occured -> return null
catch (Exception)
{
return null;
}
}
So basically using i'm m_HttpClient.GetStringAsync instead of m_HttpClient.GetAsync and then change the fluff around it to work with the different return type. If i do it like this, everything works without any problems.
Does anyone have an idea why GetAsync doesn't work properly (doesn't seem to send the 3rd request) with AutomaticDecompression, where as GetStringAsync works flawlessly?
There are bug reports about this exact issue:
https://bugzilla.xamarin.com/show_bug.cgi?id=21477
The bug is marked as RESOLVED FIXED and the recomended action is to update to the latest stable build. But there are other (newer) bugreports that indicate the same thing that are still open, ex:
https://bugzilla.xamarin.com/show_bug.cgi?id=34747
I made a workaround by implementing my own HttpHandler like so:
public class DecompressionHttpClientHandler : HttpClientHandler
{
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
var msg = await base.SendAsync(request, cancellationToken);
if (msg.Content.Headers.ContentEncoding.Contains("gzip"))
{
var compressedStream = await msg.Content.ReadAsStreamAsync();
var uncompresedStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Decompress);
msg.Content = new StreamContent(uncompresedStream);
}
return msg;
}
}
Note that the code above is just an example and not a final solution. For example the request will not be compressed and all headers will be striped from the result. But you get the idea.

unusual callback calling

I have such a problem: on device with Android OS, I am executing action script code. And I have a method, which must to send some data to sever. But!!! Even if I stop my application before sending some data to server - it still somehow sends that data. I don't understand how it can be?? Anyone faced with this problem? Please help me. Thanks.
public class VerifyCommand extends SimpleCommand
{
override public function execute(notification:INotification):void
{
//here is the place, where I put breakpoint and stop the program
trace("here is the place, where I put breakpoint and stop the program");
verify();
}
private function verify():void
{
var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();
trace(stackTrace);
var request : URLRequest = new URLRequest();
request.url = "http://www.somesite.com";
request.method = URLRequestMethod.POST;
request.contentType = "application/x-www-form-urlencoded; charset=utf-8";
request.data = variables;
request.useCache = false;
request.cacheResponse = false;
var call_loader:URLLoader = new URLLoader();
call_loader.dataFormat = URLLoaderDataFormat.TEXT;
call_loader.addEventListener(Event.COMPLETE, onVerificationResult);
call_loader.addEventListener(IOErrorEvent.IO_ERROR, onVerificationError);
call_loader.load(request);
}
private function onVerificationResult(event:Event):void
{
trace("all ok");
}
private function onVerificationError( event:Event):void
{
trace("all failed");
}
}
It was a debugger fault , it doesn't stop exactly where I think it should.

Categories

Resources