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?
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"
}
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://fakestoreapi.com/products");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "");
var response = client.GetAsync("products").Result;
var root = response.Content.ReadAsAsync<IEnumerable<Root>>().Result;
gridControl1.DataSource = root;
//Root class with API data Atributes as model
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.