Xamarin Consume REST API

edwardkchedwardkch MYMember ✭✭

Hi,

This is my first time exploring with xamarin to build a mobile application that connects to a back end system using REST API. However, I have no idea how to do it. Below are the link to the set of API that I would like to access. Hope there's someone out there that could share with me on how to achieve this.

https://docs.oracle.com/cd/E53430_01/EOTRS/op-v2-dataservice-table-{tableName}-get.html#examples

Thanks

Best Answer

  • edwardkchedwardkch MY ✭✭
    Accepted Answer

    Hi Marcus,

    I understand the modal class but not much on the RestCall in . NET. Was wondering where is the Http Client Call, I had something like this but it didnt work.

    public partial class MainPage : ContentPage
    {

        private const string Url = "http://ais:port/jderest/dataservice";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Post> _posts;
    
        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override async void OnAppearing()
        {
            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "amRlOkpERQ ==");
    
            var content = await _client.GetStringAsync(Url);
            var posts = JsonConvert.DeserializeObject<List<Post>>(content);
    
    
            _posts = new ObservableCollection<Post>(posts);
            postsListView.ItemsSource = _posts;
    
            base.OnAppearing();
        }
    

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @edwardkch

    You must read the API/documentation for the same.

    Once you are done with that, read a step by step tutorial.

    I strongly recommend the reading API first and then try the tutorial/samples.

    Hope this helps.

    Regards,
    N Baua

  • MarcusRungeMarcusRunge DEMember ✭✭✭

    This will be your Model Class according to your link where you deserialize your data in:

    public class Columns
        {
            public string F0101_AN8 { get; set; }
            public string F0101_ALKY { get; set; }
            public string F0101_TAX { get; set; }
            public string F0101_ALPH { get; set; }
            public string F0101_DC { get; set; }
            public string F0101_MCU { get; set; }
            public string F0101_SIC { get; set; }
            public string F0101_LNGP { get; set; }
            public string F0101_AT1 { get; set; }
            public string F0101_CM { get; set; }
            public string F0101_TAXC { get; set; }
            public string F0101_AT2 { get; set; }
            public string F0101_AT3 { get; set; }
            public string F0101_AT4 { get; set; }
            public string F0101_AT5 { get; set; }
            public string F0101_ATP { get; set; }
            public string F0101_ATR { get; set; }
            public string F0101_ATPR { get; set; }
            public string F0101_AB3 { get; set; }
            public string F0101_ATE { get; set; }
            public string F0101_SBLI { get; set; }
            public string F0101_EFTB { get; set; }
            public string F0101_AN81 { get; set; }
            public string F0101_AN82 { get; set; }
            public string F0101_AN83 { get; set; }
            public string F0101_AN84 { get; set; }
            public string F0101_AN86 { get; set; }
            public string F0101_AN85 { get; set; }
            public string F0101_AC01 { get; set; }
            public string F0101_AC02 { get; set; }
            public string F0101_AC03 { get; set; }
            public string F0101_AC04 { get; set; }
            public string F0101_AC05 { get; set; }
            public string F0101_AC06 { get; set; }
            public string F0101_AC07 { get; set; }
            public string F0101_AC08 { get; set; }
            public string F0101_AC09 { get; set; }
            public string F0101_AC10 { get; set; }
            public string F0101_AC11 { get; set; }
            public string F0101_AC12 { get; set; }
            public string F0101_AC13 { get; set; }
            public string F0101_AC14 { get; set; }
            public string F0101_AC15 { get; set; }
            public string F0101_AC16 { get; set; }
            public string F0101_AC17 { get; set; }
            public string F0101_AC18 { get; set; }
            public string F0101_AC19 { get; set; }
            public string F0101_AC20 { get; set; }
            public string F0101_AC21 { get; set; }
            public string F0101_AC22 { get; set; }
            public string F0101_AC23 { get; set; }
            public string F0101_AC24 { get; set; }
            public string F0101_AC25 { get; set; }
            public string F0101_AC26 { get; set; }
            public string F0101_AC27 { get; set; }
            public string F0101_AC28 { get; set; }
            public string F0101_AC29 { get; set; }
            public string F0101_AC30 { get; set; }
            public string F0101_GLBA { get; set; }
            public string F0101_PTI { get; set; }
            public string F0101_PDI { get; set; }
            public string F0101_MSGA { get; set; }
            public string F0101_RMK { get; set; }
            public string F0101_TXCT { get; set; }
            public string F0101_TX2 { get; set; }
            public string F0101_ALP1 { get; set; }
            public string F0101_URCD { get; set; }
            public string F0101_URDT { get; set; }
            public string F0101_URAT { get; set; }
            public string F0101_URAB { get; set; }
            public string F0101_URRF { get; set; }
            public string F0101_USER { get; set; }
            public string F0101_PID { get; set; }
            public string F0101_UPMJ { get; set; }
            public string F0101_JOBN { get; set; }
            public string F0101_UPMT { get; set; }
            public string F0101_PRGF { get; set; }
            public string F0101_SCCLTP { get; set; }
            public string F0101_TICKER { get; set; }
            public string F0101_EXCHG { get; set; }
            public string F0101_DUNS { get; set; }
            public string F0101_CLASS01 { get; set; }
            public string F0101_CLASS02 { get; set; }
            public string F0101_CLASS03 { get; set; }
            public string F0101_CLASS04 { get; set; }
            public string F0101_CLASS05 { get; set; }
            public string F0101_NOE { get; set; }
            public string F0101_GROWTHR { get; set; }
            public string F0101_YEARSTAR { get; set; }
            public string F0101_AEMPGP { get; set; }
            public string F0101_ACTIN { get; set; }
            public string F0101_REVRNG { get; set; }
            public string F0101_SYNCS { get; set; }
            public string F0101_PERRS { get; set; }
            public string F0101_CAAD { get; set; }
        }
    
        public class Rowset
        {
            public string F0101_DC { get; set; }
            public string F0101_ATP { get; set; }
            public object F0101_EFTB { get; set; }
            public string F0101_JOBN { get; set; }
            public string F0101_AC19 { get; set; }
            public string F0101_AC18 { get; set; }
            public string F0101_ALP1 { get; set; }
            public string F0101_USER { get; set; }
            public string F0101_MCU { get; set; }
            public string F0101_ATR { get; set; }
            public string F0101_AC20 { get; set; }
            public string F0101_TAX { get; set; }
            public string F0101_GLBA { get; set; }
            public string F0101_PRGF { get; set; }
            public int F0101_GROWTHR { get; set; }
            public string F0101_ALKY { get; set; }
            public string F0101_CM { get; set; }
            public int F0101_AN81 { get; set; }
            public string F0101_AC26 { get; set; }
            public string F0101_AC25 { get; set; }
            public int F0101_AN83 { get; set; }
            public string F0101_AC28 { get; set; }
            public int F0101_AN82 { get; set; }
            public string F0101_AC27 { get; set; }
            public string F0101_AC22 { get; set; }
            public string F0101_AC21 { get; set; }
            public string F0101_AC24 { get; set; }
            public string F0101_YEARSTAR { get; set; }
            public string F0101_ALPH { get; set; }
            public string F0101_AC23 { get; set; }
            public string F0101_CLASS02 { get; set; }
            public string F0101_CLASS01 { get; set; }
            public string F0101_ACTIN { get; set; }
            public string F0101_AC08 { get; set; }
            public string F0101_AC07 { get; set; }
            public string F0101_CLASS05 { get; set; }
            public string F0101_CLASS04 { get; set; }
            public string F0101_AC09 { get; set; }
            public string F0101_CLASS03 { get; set; }
            public int F0101_SYNCS { get; set; }
            public int F0101_NOE { get; set; }
            public string F0101_TX2 { get; set; }
            public string F0101_SIC { get; set; }
            public int F0101_AN8 { get; set; }
            public string F0101_AC15 { get; set; }
            public string F0101_AC14 { get; set; }
            public string F0101_AC17 { get; set; }
            public string F0101_EXCHG { get; set; }
            public string F0101_AEMPGP { get; set; }
            public string F0101_AC16 { get; set; }
            public string F0101_URCD { get; set; }
            public string F0101_AB3 { get; set; }
            public string F0101_AC11 { get; set; }
            public string F0101_AC10 { get; set; }
            public string F0101_AC13 { get; set; }
            public string F0101_AC12 { get; set; }
            public string F0101_TAXC { get; set; }
            public string F0101_URRF { get; set; }
            public int F0101_PTI { get; set; }
            public double F0101_URAT { get; set; }
            public string F0101_DUNS { get; set; }
            public string F0101_SBLI { get; set; }
            public int F0101_PERRS { get; set; }
            public string F0101_AC04 { get; set; }
            public string F0101_AC03 { get; set; }
            public object F0101_PDI { get; set; }
            public string F0101_AC06 { get; set; }
            public string F0101_AC05 { get; set; }
            public string F0101_AC02 { get; set; }
            public string F0101_AC01 { get; set; }
            public string F0101_SCCLTP { get; set; }
            public string F0101_AT1 { get; set; }
            public string F0101_UPMJ { get; set; }
            public string F0101_TXCT { get; set; }
            public int F0101_AN85 { get; set; }
            public int F0101_AN84 { get; set; }
            public string F0101_AC29 { get; set; }
            public int F0101_AN86 { get; set; }
            public string F0101_LNGP { get; set; }
            public string F0101_ATPR { get; set; }
            public object F0101_URDT { get; set; }
            public string F0101_AT5 { get; set; }
            public string F0101_AT4 { get; set; }
            public string F0101_AT3 { get; set; }
            public string F0101_AT2 { get; set; }
            public string F0101_AC30 { get; set; }
            public string F0101_PID { get; set; }
            public int F0101_UPMT { get; set; }
            public int F0101_URAB { get; set; }
            public string F0101_ATE { get; set; }
            public string F0101_TICKER { get; set; }
            public string F0101_REVRNG { get; set; }
            public string F0101_MSGA { get; set; }
            public string F0101_RMK { get; set; }
            public int F0101_CAAD { get; set; }
        }
    
        public class Summary
        {
            public int records { get; set; }
            public bool moreRecords { get; set; }
        }
    
        public class GridData
        {
            public int id { get; set; }
            public string fullGridId { get; set; }
            public Columns columns { get; set; }
            public IList<Rowset> rowset { get; set; }
            public Summary summary { get; set; }
        }
    
        public class Data
        {
            public GridData gridData { get; set; }
        }
    
        public class FsDATABROWSEF0101
        {
            public string title { get; set; }
            public Data data { get; set; }
            public IList<object> errors { get; set; }
            public IList<object> warnings { get; set; }
        }
    
        public class EdwardsTable
        {
            public FsDATABROWSEF0101 fs_DATABROWSE_F0101 { get; set; }
            public int stackId { get; set; }
            public int stateId { get; set; }
            public string rid { get; set; }
            public string currentApp { get; set; }
            public string timeStamp { get; set; }
            public IList<object> sysErrors { get; set; }
            public int totalMS { get; set; }
            public int renderMS { get; set; }
        }
    
  • MarcusRungeMarcusRunge DEMember ✭✭✭

    Rest Call in .NET, adjustsments may be necassery:

    public async Task<EdwardsTable> GetEdwardsTable()
            {
                var httpWebResponse = await GetHttpWebResponse(@"http://ais_server_url/jderest/v2/dataservice/table/F0101?$filter=F0101.AN8%20EQ%206001");
                if (httpWebResponse != null)
                {
                    var stream = httpWebResponse.GetResponseStream();
                    var streamReader = new StreamReader(stream);
                    string response = streamReader.ReadToEnd();
                    return JsonConvert.DeserializeObject<EdwardsTable>(response);
                }
                return null;
            }
    
    async Task<HttpWebResponse> GetHttpWebResponse()
            {
                var webRequest = WebRequest.Create(domain);
                webRequest.Method = "GET";
                webRequest.Headers.Add("Authorization", "Basic SkRFOkpERQ==");
                try
                {
                    return await webRequest.GetResponseAsync() as HttpWebResponse;
                }
                catch (Exception)
                {
                    return null;
                }
            }
    
  • MarcusRungeMarcusRunge DEMember ✭✭✭

    in UWP (different):

    public async Task<EdwardsTable> GetEdwardsTable()        {
                var httpResponseMessage = await GetHttpResponseMessage(@"http://ais_server_url/jderest/v2/dataservice/table/F0101?$filter=F0101.AN8%20EQ%206001");
                if (httpResponseMessage != null)
                {
                    Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync();
                    StreamReader streamReader = new StreamReader(stream);
                    string response = streamReader.ReadToEnd();
                    return JsonConvert.DeserializeObject<EdwardsTable>(response);
                }
                return null;
            }
    
    async Task<HttpResponseMessage> GetHttpResponseMessage)
            {
                var httpClient = new HttpClient();
                var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, domain);
                httpRequestMessage.Headers.Add("Authorization", "Basic SkRFOkpERQ==");            try
                {
                    return await httpClient.SendAsync(httpRequestMessage);
                }
                catch (Exception)
                {
                    return null;
                }
            }
    
  • MarcusRungeMarcusRunge DEMember ✭✭✭

    In an UWP Background Task (all classes sealed) you have to return an IAsyncOperation and get the result with .AsTask().GetAwaiter().GetResult()

  • edwardkchedwardkch MYMember ✭✭
    Accepted Answer

    Hi Marcus,

    I understand the modal class but not much on the RestCall in . NET. Was wondering where is the Http Client Call, I had something like this but it didnt work.

    public partial class MainPage : ContentPage
    {

        private const string Url = "http://ais:port/jderest/dataservice";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Post> _posts;
    
        public MainPage()
        {
            InitializeComponent();
        }
    
        protected override async void OnAppearing()
        {
            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "amRlOkpERQ ==");
    
            var content = await _client.GetStringAsync(Url);
            var posts = JsonConvert.DeserializeObject<List<Post>>(content);
    
    
            _posts = new ObservableCollection<Post>(posts);
            postsListView.ItemsSource = _posts;
    
            base.OnAppearing();
        }
    
Sign In or Register to comment.