解析Google Directions API时出错(指定的演员表无效)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析Google Directions API时出错(指定的演员表无效)相关的知识,希望对你有一定的参考价值。
我是xamarin的新手。我想用json对Google Directions Api的回复进行解析。我为我的对象创建了一个类:
public class GeocodedWaypoint
{
public string geocoder_status { get; set; }
public string place_id { get; set; }
public List<string> types { 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 Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Distance
{
public string text { get; set; }
public int value { get; set; }
}
public class Duration
{
public string text { get; set; }
public int value { get; set; }
}
public class EndLocation
{
public double lat { get; set; }
public double lng { get; set; }
}
public class StartLocation
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Distance2
{
public string text { get; set; }
public int value { get; set; }
}
public class Duration2
{
public string text { get; set; }
public int value { get; set; }
}
public class EndLocation2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Polyline
{
public string points { get; set; }
}
public class StartLocation2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Step
{
public Distance2 distance { get; set; }
public Duration2 duration { get; set; }
public EndLocation2 end_location { get; set; }
public string html_instructions { get; set; }
public Polyline polyline { get; set; }
public StartLocation2 start_location { get; set; }
public string travel_mode { get; set; }
public string maneuver { get; set; }
}
public class Leg
{
public Distance distance { get; set; }
public Duration duration { get; set; }
public string end_address { get; set; }
public EndLocation end_location { get; set; }
public string start_address { get; set; }
public StartLocation start_location { get; set; }
public List<Step> steps { get; set; }
public List<object> traffic_speed_entry { get; set; }
public List<object> via_waypoint { get; set; }
}
public class OverviewPolyline
{
public string points { get; set; }
}
public class Route
{
public Bounds bounds { get; set; }
public string copyrights { get; set; }
public List<Leg> legs { get; set; }
public OverviewPolyline overview_polyline { get; set; }
public string summary { get; set; }
public List<object> warnings { get; set; }
public List<object> waypoint_order { get; set; }
}
public class DirectionsDto
{
public List<GeocodedWaypoint> geocoded_waypoints { get; set; }
public List<Route> routes { get; set; }
public string status { get; set; }
}
我试图像这样反序列化响应:
JsonValue json = await FetchDataAsync(url);
DirectionsDto directions = JsonConvert.DeserializeObject<DirectionsDto>(json);
private async Task<String> FetchDataAsyncXML(string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/xml";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
return response.GetResponseStream().ToString();
}
}
}
我收到此错误:System.InvalidCastException:指定的强制转换无效。我只需要整个回复中的一件事。我只需要OverviewPolyline。这是在回应的最后。如果使用xml更容易,那么我认为也可以使用xml。
我被困在这里,非常令人沮丧。
答案
将JsonValue
更改为string
,因为FetchDataAsyncXML
中返回值的类型是string
:
string json = await FetchDataAsyncXML("https://maps.googleapis.com/maps/api/directions/json?origin=44.439663,26.096306&destination=44.8564798,24.8691824");
android.Util.Log.Error("lv", json);
DirectionsDto directions = JsonConvert.DeserializeObject<DirectionsDto>(json);
Android.Util.Log.Error("lv=========", directions.status);
在FetchDataAsyncXML
方法中这样做:
private async Task<String> FetchDataAsyncXML(string url)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/xml";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
//return response.GetResponseStream().ToString();
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
//retrun html code
string content = sr.ReadToEnd();
Android.Util.Log.Error("lv++", content);
return content;
}
}
}
以上是关于解析Google Directions API时出错(指定的演员表无效)的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps API [Directions API] 航点限制?
Google Maps API DirectionsService.route 与 Google Maps Directions 不同
如何从 Google Maps Directions Api 获取折线
如何让 Google Maps Directions API 选择正确的机场?