Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - response.Content.ReadAsAsync<Result> not working as I think it should work

Tags:

c#

.net

I am working on a winform app, and I am doing an API call to the GoogleMaps API. But for some reason I cannot get a response. Or actually, I do get a response but cannot do anything with it.

My code:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GoogleMaps
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();

            setup().Wait();
        }

        static HttpClient client = new HttpClient();

        static async Task setup()
        {
            client.BaseAddress = new Uri("https://maps.googleapis.com/maps/api/geocode/json");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        }

        static async Task<Result> getAdress(int huisnummer, string postcode)
        {
            Result address = null;
            HttpResponseMessage response = await client.GetAsync($"?address=00+1234AB&key=MYKEY");
            if (response.IsSuccessStatusCode)
            {
                address = await response.Content.ReadAsAsync<Result>();
            }
            return address;
        }

        private async void BTN_submit_Click(object sender, EventArgs e)
        {
            Result address = new Result();

            address = await getAdress(31, "8256SC");

            richTextBox1.Text = "address is:" + address.formatted_address;
        }
    }
}

And my "Paste JSON as Classes" classes:

namespace GoogleMaps
{

    public class Rootobject
    {
        public Result[] results { get; set; }
        public string status { get; set; }
    }

    public class Result
    {
        public Address_Components[] address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public string place_id { get; set; }
        public string[] types { get; set; }
    }

    public class Geometry
    {
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }

    public class Location
    {
        public float lat { get; set; }
        public float lng { get; set; }
    }

    public class Viewport
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }

    public class Northeast
    {
        public float lat { get; set; }
        public float lng { get; set; }
    }

    public class Southwest
    {
        public float lat { get; set; }
        public float lng { get; set; }
    }

    public class Address_Components
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public string[] types { get; set; }
    }

}

My output right now is just nothing. But I do get a 200 response. How do I fix this?

like image 707
Luuk Wuijster Avatar asked Apr 29 '17 19:04

Luuk Wuijster


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

You are deserializing into the incorrect class. The correct class is Rootobject

address = await response.Content.ReadAsAsync<Rootobject>();

instead of using Result

address = await response.Content.ReadAsAsync<Result>();

Sample Geocoding API response (from Google API Docs):

{
    "results" : [
     {
         "address_components" : [
         {
            "long_name" : "1600",
            "short_name" : "1600",
            "types" : [ "street_number" ]
         }]
    ...            
    }],
   "status" : "OK"
}
like image 161
degant Avatar answered Oct 02 '22 14:10

degant