Argument1: Cannot convert from 'System.IO.Stream' to 'String'

I'm stumped on this, and could use some assistance. I've done web searches, and read documentation for the last several hours, and have had no luck.

I'm getting an error that reads as follows from the "response" overload entry of the "JObject o = JObject.Parse(response);" line.

Argument1: cannot convert from 'System.IO.Stream' to 'string'

static void MyFunction(out string Value1, out string Value2)
{
    HttpClient client = new HttpClient();
    var response = client.GetStreamAsync("My URI").Result;
    JObject o = JObject.Parse(response);
    Value1 = (string)o.SelectToken("PressureReading");
    Value2 = (string)o.SelectToken("PressureTrend");
}

I had this code working using webclient in a console app project. However, since this is a for UWP project, I'm unable to use webclient (and am required to use HttpClient). Also, the JSON string I'm parsing from my REST API is as follows:

{"ID":8,"Site":"EstevanPointCanada","PressureReading":"30.05     ","PressureTrend":"0         "}

What changes do I need to make in order for the above function to compile?

Thanks in advance for any assistance provided.

Jon Skeet
people
quotationmark

JObject.Parse takes a string, not a Stream. You're trying to pass it response, which is a Stream.

To fix it, just use HttpClient.GetStringAsync instead, e.g.

using (HttpClient client = new HttpClient())
{
    var response = client.GetStringAsync("My URI").Result;
    JObject o = JObject.Parse(response);
    Value1 = (string)o.SelectToken("PressureReading");
    Value2 = (string)o.SelectToken("PressureTrend");
}

Note that if you find yourself getting confused by errors like this, it's worth making all the types explicit - if you'd used explicit typing for response instead of var, it would have been very obvious that either you'd expected it to be a string and it wasn't, or that you'd expected it to be a Stream but that JObject.Parse didn't accept a stream...

people

See more on this question at Stackoverflow