locked
How can Xamarin.Forms class access JSON from REST WCF service? RRS feed

  • Question

  • User382076 posted

    Hi,

    I'm learning all about Xamarin.Forms, so please bear with me. This may seem like a simple question, but finding a solution has been an uphill battle.

    This code snippet is from a class in my Xamarin.Forms project that loops through a json stream. I use the Newtonsoft library for the json:

    string ar = string ar = "[{\"lon\":-66.105700,\"lat\":18.466333},{\"lon\":-65.827300,\"lat\":18.149683},{\"lon\":-65.961975,\"lat\":18.380102}]";
    var obj = JsonConvert.DeserializeObject(ar);
    string lat = string.Empty;
    string lon = string.Empty;
    
    foreach (var item in ((JArray)obj))
    {
        lat = item.Value<string>("lat");
        lon = item.Value<string>("lon");
    }
    

    This works as expected.

    Now, instead of being that string, it will be the string returned by calling URL http//IP/Service1.svc/GetTableData (the forum doesn't let me post URLs). This URL will return the same string format that was used in the snippet above:

    "[{\"lon\":-66.105721,\"lat\":18.466333},{\"lon\":-65.827385,\"lat\":18.149683},{\"lon\":-65.961975,\"lat\":18.380102}]"
    

    My question: How can my class in the Xamarin.Forms project call the URL and read the string being returned? I only have access to the URL, so I can't add it to my project as a service reference.

    I'm using Visual Studio 2017.

    Any help is appreciated.

    Saturday, February 2, 2019 9:42 PM

Answers

  • User380718 posted

    Hi, you could use HttpClient to request the URL and get the Json.

    like this :

      HttpClient client = new HttpClient ();
      // ResultUrl  = "http//IP/Service1.svc/GetTableData"
      var uri = new Uri (string.Format (ResultUrl, string.Empty));
      var response = await client.GetAsync (uri);
      if (response.IsSuccessStatusCode) {
          var content = await response.Content.ReadAsStringAsync ();
          string lat = string.Empty;
          string lon = string.Empty;
          foreach (var item in ((JArray)content ))
             {
               lat = item.Value<string>("lat");
               lon = item.Value<string>("lon");
            }
       }
    

    You could refer to RESTful Web Service for encapsulating

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, February 3, 2019 2:30 AM

All replies

  • User380718 posted

    Hi, you could use HttpClient to request the URL and get the Json.

    like this :

      HttpClient client = new HttpClient ();
      // ResultUrl  = "http//IP/Service1.svc/GetTableData"
      var uri = new Uri (string.Format (ResultUrl, string.Empty));
      var response = await client.GetAsync (uri);
      if (response.IsSuccessStatusCode) {
          var content = await response.Content.ReadAsStringAsync ();
          string lat = string.Empty;
          string lon = string.Empty;
          foreach (var item in ((JArray)content ))
             {
               lat = item.Value<string>("lat");
               lon = item.Value<string>("lon");
            }
       }
    

    You could refer to RESTful Web Service for encapsulating

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, February 3, 2019 2:30 AM
  • User382076 posted

    Thanks.

    Sunday, February 3, 2019 6:16 PM