I am using rest api to get a list of places from a given location using google maps but i'm having some issues .
Main Issue : While the response is ok most of the time , sometimes i get a wrongly formatted json or status="Not Ok" which cause my app to crash :
Is there a way to test for the status & validity of the json before deserializing to prevent crashes or other way ?
System.NullReferenceException: Object reference not set to an instance of an object.
from this line :
place = JsonConvert.DeserializeObject<Places>(content,settings);
Secondary issue : I'm not sure what is the best way to deal with the page token returned and how to use it for recurring queries
Code for getting the json ( The try-catch does nothing - app still crashes)
public async Task<Places> recurNearbyPlacesRequest(int radius , string pageToken)
{
HttpClient client = new HttpClient();
Places place = null;
string testurl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.498358,-73.4721847&radius=2000&pagetoken={0}&key={1}";
var uri = new Uri(string.Format(testurl, pageToken , Constants.google_maps_key));
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
place = JsonConvert.DeserializeObject<Places>(content,settings);
}
}
catch (Exception E) { }
return place;
}
Error Json that causes crash :
{
"error_message" : "The provided API key is invalid.",
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}
{
"html_attributions" : [],
"results" : [],
"status" : "INVALID_REQUEST"
}
Places Class :
//JsontoC#
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public Viewport viewport { get; set; }
}
public class OpeningHours
{
public bool open_now { get; set; }
public List<object> weekday_text { get; set; }
}
public class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
public class Result
{
public Geometry geometry { get; set; }
public string icon { get; set; }
public string id { get; set; }
public string name { get; set; }
public OpeningHours opening_hours { get; set; }
public List<Photo> photos { get; set; }
public string place_id { get; set; }
public double rating { get; set; }
public string reference { get; set; }
public string scope { get; set; }
public List<string> types { get; set; }
public string vicinity { get; set; }
public int? price_level { get; set; }
}
public class RootObject
{
public List<object> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<Result> results { get; set; }
public string status { get; set; }
}