Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How to catch problems with WebService in iOS and Android properly?

$
0
0

In my app (based on template “Blank App (Xamarin.forms Shared)”), I call our WebService (JSON).
Installed version of Xamarin.Forms: 1.2.2.6243.

With the WebService everything works as expected (with all platforms)…

I now want to catch the case, that our server don’t run ("error-path").

With the code below (and a wrong url), only the WP-Emulator run’s as expected.

  • Try to call call the WebService

  • response.IsSuccessStatusCode is settled to false

  • give back null

  • display alarm “Error calling Webservice…”

I seems, as on iOS the statement If (! Response.IsSucessStatusCode) is not reached -> Crash

On Android, the behavior seems similar

How to handle this also with iOS and Android?

Thanks

Code snipped calling WebService over function GetSucheAsync():

    var es = await sv.GetSucheAsync(cURL); 
    if (es == null)
    {
      await this.DisplayAlert("Error calling WebService", "Please check Internet-Connection and try again ", "OK");
     return;
    }


**Code snipped from GetSucheAsync():**


var client = new System.Net.Http.HttpClient();

var response = await client.GetAsync(cURL); 
if (! response.IsSuccessStatusCode) 
{
  return null;
};
var EmpfehlungenJson = response.Content.ReadAsStringAsync().Result; 
try
{
  List<Suchergebnis> RootobjectSuche =      JsonConvert.DeserializeObject<List<Suchergebnis>>(EmpfehlungenJson);
return RootobjectSuche;
}

catch (Exception e)
{
  return null;
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>