locked
How do I get the information from an api? RRS feed

  • Question

  • User379050 posted

    Hey there. I am new to this topic and a bit confused. I used https://strapi.io/ to create an api. I got this link for me "http://localhost:1337/NewsTests" with this content:

    "[{"titel":"lorem ipsum","date":"2019-01-17T12:00:00.000Z","text":"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.","id":"5c41b03ddebbc8624780986a","createdAt":"2019-01-18T10:53:49.138Z","updatedAt":"2019-01-18T10:53:49.165Z","_v":0,"id":"5c41b03ddebbc8624780986a"}]"

    And I dont know how to get that information to my Xamarin.Forms Crossplatform app.

    I read trough this but as far as I can tell it is not a crossplatform app.

    Can it be done as a crossplatform app? At first I thought it would be a simple task. Some kind of "take the link and get the title of the content" (this is what I actually just want to do atm). But after reading trough the documentation and looking at the sample code I got overwhelmed.

    Friday, January 18, 2019 12:59 PM

Answers

  • User235050 posted

    Yes, it's definitely doable on crossplatform. Probably the easiest way is to use this nuget package: https://www.nuget.org/packages/Microsoft.Net.Http

    and then use the HttpClient class to get the API response var httpClient = new HttpClient(); httpClient.GetAsync(...)

    regards

    Jiri

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 18, 2019 1:58 PM
  • User364658 posted

    Hi @axbeit It seems your web service has not yet been published "http: // localhost: 1337 / NewsTests" since you the url it would be "http: // localhost / NewsTests" I recommend you these two post for publish your service in a local IIS 1. https://luisdavidxamshap.wordpress.com/2018/01/25/publicar-web-service-cwf-en-ambiente-iis-local-paso-a-paso/ 2. https://luisdavidxamshap.wordpress.com/2018/07/13/publicar-api-rest-net-core-en-iis-local/

    And you can use HttpClient as @JiriMatejka said or HttpWebResponse(not asynchronous) In this post you can check how to implement HttpWebResonse https://luisdavidxamshap.wordpress.com/2018/04/03/consumir-web-api-en-xamarin-forms/ and this one how to use HttpClient https://luisdavidxamshap.wordpress.com/2018/02/21/consumir-api-rest-en-xamarin-forms/

    and another thing very important is: when you're consuming a local web service you must to use your local ip not "localhost" in example

    using System.Net.Http;
    using Newtonsoft.Json;
    
    private static readonly HttpClient client = new HttpClient();
    in your method 
    async void consuming()
    {
    var responseString = await client.GetStringAsync("http://192.168.2.7/NewsTests");
    .....
    deserealize the information into your model 
    }
    

    hope be useful for you :smile:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 18, 2019 7:29 PM

All replies

  • User235050 posted

    Yes, it's definitely doable on crossplatform. Probably the easiest way is to use this nuget package: https://www.nuget.org/packages/Microsoft.Net.Http

    and then use the HttpClient class to get the API response var httpClient = new HttpClient(); httpClient.GetAsync(...)

    regards

    Jiri

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 18, 2019 1:58 PM
  • User364658 posted

    Hi @axbeit It seems your web service has not yet been published "http: // localhost: 1337 / NewsTests" since you the url it would be "http: // localhost / NewsTests" I recommend you these two post for publish your service in a local IIS 1. https://luisdavidxamshap.wordpress.com/2018/01/25/publicar-web-service-cwf-en-ambiente-iis-local-paso-a-paso/ 2. https://luisdavidxamshap.wordpress.com/2018/07/13/publicar-api-rest-net-core-en-iis-local/

    And you can use HttpClient as @JiriMatejka said or HttpWebResponse(not asynchronous) In this post you can check how to implement HttpWebResonse https://luisdavidxamshap.wordpress.com/2018/04/03/consumir-web-api-en-xamarin-forms/ and this one how to use HttpClient https://luisdavidxamshap.wordpress.com/2018/02/21/consumir-api-rest-en-xamarin-forms/

    and another thing very important is: when you're consuming a local web service you must to use your local ip not "localhost" in example

    using System.Net.Http;
    using Newtonsoft.Json;
    
    private static readonly HttpClient client = new HttpClient();
    in your method 
    async void consuming()
    {
    var responseString = await client.GetStringAsync("http://192.168.2.7/NewsTests");
    .....
    deserealize the information into your model 
    }
    

    hope be useful for you :smile:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 18, 2019 7:29 PM