Results 1 to 3 of 3

Thread: HttpClient

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    HttpClient

    I'm playing around with trying to get JSON data from an online source. It'll be the backbone of a little project I'm keen to build.

    I'm getting a response when I run the application with errors commented out so I'm connecting okay.

    In my code editor, responseString below has a red squiggly line, when I hover it says "'responseString' is not null here"

    When I run the application I get

    "Argument 1: cannot convert from 'System.Threading.Tasks.Task<string>' to 'System.IO.Stream'"

    I've tried using NewtonSoft.Json as well, same response.

    Any ideas?

    Code:
            private async Task getPlayersAPI()
            {
                List<Player> playerList = new List<Player>();
                using (var httpClient = new HttpClient())
                {
                    var response = await httpClient.GetAsync( _url );
                    var responseString = response.Content.ReadAsStringAsync();
                    playerList = JsonSerializer.Deserialize<List<Player>>(responseString);
                    //playerList = JsonConvert.DeserializeObject<List<Player>>(responseString);
                    testData.Text = responseString.ToString();
                }

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,828

    Re: HttpClient

    Try
    Code:
        var responseString = await response.Content.ReadAsStringAsync();
    https://learn.microsoft.com/en-us/do...s-programming/
    and
    https://learn.microsoft.com/en-us/do...perators/await

    are probably worth a look as they explain async methods and the await keyword.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2012
    Posts
    440

    Re: HttpClient

    Sorry posted in VB.net not C# thanks for moving.

    Yes, I was missing the await keyword. Just came back to update that and found the responses.

    I'm not getting "Converting null literal or possible null value to non-nullable type." over this line,

    Code:
    playerList = JsonSerializer.Deserialize<List<Player>>(responseString);
    And via try catch getting this when running the application

    Code:
    System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List`1[MyProgram.Classes.Player]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
       at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
       at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
       at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
       at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
       at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
       at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
       at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
       at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
       at MyProgram.PlayerList.getPlayersAPI() in C:\Users\bevan\source\repos\MyProgram\MyProgram\Pages\PlayerList.xaml.cs:line 55
    My Player object is very simple

    Code:
        public class Player
        {
    
            public int Id {  get; set; }
            public required string fullName { get; set; }
    
        }
    JSON is like this

    HTML Code:
    {"Players": [{"Id":5,"FullName":"Thomas Smith"},{"Id":74,"FullName":"John Anderson"}]}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width