我正在学习Xamarin表单。我正在使用Google Place API for searching nearby
我已经设法在.NET中调用了API,但我只得到了20个结果,因为它是写在Google文档中的。
下面是我的代码:
string lati = location.Latitude.ToString().Replace(",", ".");
string longi = location.Longitude.ToString().Replace(",", ".");
string latitude = lati;
string longitude = longi;
string Myradius = "3000";
var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
// List data response.
HttpResponseMessage response = await client.GetAsync(urlParameters);
if (response.IsSuccessStatusCode)
{
JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
JArray MyJsonResult = (JArray)joResponse["results"];
// I want to have the second page result of my API Call
string token = joResponse["next_page_token"].ToString(); // I have manage to get the token
JArray MyJsonResult2 = await GetnextPageData(latitude, longitude, Myradius, token); //no result
}
async Task<JArray> GetnextPageData(string latitude, string longitude, string Myradius , string token)
{
var URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?";
string urlParameters = "?location=" + latitude + "," + longitude + "&radius=" + Myradius + "&key=My_GOOGLE_KEY&pagetoken=" + token;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// List data response
HttpResponseMessage response = await client.GetAsync(urlParameters);
Console.WriteLine("MyPrint " + response);
if (response.IsSuccessStatusCode)
{
JObject joResponse = JObject.Parse(await response.Content.ReadAsStringAsync());
Console.WriteLine("MyPrint " + response);
JArray MyJsonResult = (JArray)joResponse["results"];
if (MyJsonResult != null)
{
return MyJsonResult;
}
else
{
return null;
}
}
return null;
}
我已经找到了这些链接,但仍然无法为结果的其余部分添加参数:
Obtaining more than 20 results with Google Places API
How to get 20+ result from Google Places API?
问题:我设法获得了令牌,但当我将其放入参数时,没有得到任何结果-MyJsonResult2
为空。
当我打印第二个呼叫的“响应”时,我看到了:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.NSUrlSessionHandler+NSUrlSessionDataTaskStreamContent, Headers:
{
server-timing: gfet4t7; dur=30
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Server: scaffolding
Server: on
Server: HTTPServer2
x-xss-protection: 0
Cache-Control: public, max-age=300
Date: Sun, 13 Nov 2022 02:19:16 GMT
X-Frame-Options: SAMEORIGIN
Vary: Accept-Language
Content-Type: application/json; charset=UTF-8
server-timing: gfet4t7; dur=30
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Content-Encoding: gzip
x-xss-protection: 0
Expires: Sun, 13 Nov 2022 02:24:16 GMT
Content-Length: 86
X-Frame-Options: SAMEORIGIN
}
当我为第二个调用打印“joResponse”时,我得到了以下内容:
{
"html_attributions": [],
"results": [],
"status": "INVALID_REQUEST"
}
谢谢你的帮助。
1条答案
按热度按时间yyyllmsg1#
我已经找到了解决方案。我必须在我的请求中添加参数:
"hasNextPage=true&nextPage()=true"