0

I have a very strange problem, I programmed an app that triggers a webservice and gives me a response back. This also works on android and IOS (in debug mode) but when I install the app via adhoc it still works but when I press the button that triggers the webservice the app freezes and you have to restart it but it always stops at the same place.

I have found out with a lot of commenting and commenting out that the error happens in the marked place. In debug mode everything works as it should.

Here is the code that will be executed.

IsMYSite(strLink);

if (gotResponse)
{
    gotResponse = false;
    DetailPage.MYPages.Add(new MYPage { Title = portal, Link = strLink, Username = "", Password = "", IsVisible = false, CompanyImage = ImageLink });
    App.Current.Properties["MYPages"] = JsonConvert.SerializeObject(DetailPage.MYPages);

    App.Current.Properties["LastPortal"] = strLink;

    Navigation.PopAsync(true);

    LinkURL.TextColor = Color.Black;
}
else
{
    LinkURL.TextColor = Color.Red;
}

public async void IsMYSite(String url)
{
    try
    {
        String linkonly = url;
        url += "/api/" + CultureInfo.CurrentUICulture.ThreeLetterWindowsLanguageName;
        string strpost = "";
        StringContent str = new StringContent(strpost, Encoding.UTF8, "application/x-www-form-urlencoded");
        var client = new System.Net.Http.HttpClient();
        client.Timeout = new TimeSpan(0, 0, 6);
        client.BaseAddress = new Uri(url);

        var response = await client.GetAsync(new Uri(url));


        Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);

        portal = result["PortalTitle"];

        if (result["LoginLogo_Path"].StartsWith("~"))
        {
            StringBuilder str2 = new StringBuilder(result["LoginLogo_Path"]);
            str2.Remove(0, 1);
            ImageLink = linkonly + str2.ToString();
            gotResponse = true;
        }
    }
    catch
    {

    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Markus3D
  • 77
  • 1
  • 1
  • 11
  • Assuming you are deadlocking, use `await` instead of `.Result`, there are lots of SO Q/As concerning this, i.e. https://stackoverflow.com/a/24657079/4984832 (this answer links to a couple of blog posts by Stephen Cleary....) – SushiHangover Jul 25 '18 at 10:34
  • Thanks first of all but I changed the code to this : var response = await client.GetAsync(new Uri(url)); but this dont work because i get no response – Markus3D Jul 25 '18 at 11:41
  • Check the device log to see what is happening...also the default timeout is ~100 seconds, so are you actually timing out if you wait 2 minutes? – SushiHangover Jul 25 '18 at 11:47
  • No it's happening directly. I added more code so you can see how my app is structured. Maybe its a logic bug because I'm new in this xamarin scene :) – Markus3D Jul 25 '18 at 11:58
  • Have you provided permissions of Internet in Android and iOS projects? – Srusti Thakkar Jul 26 '18 at 05:36
  • Yes I have all permissions but it won't work anyways :/ – Markus3D Jul 26 '18 at 06:44
  • try changing async void to async Task and await it – Patrick Goode Jul 28 '18 at 16:30

0 Answers0