android - Google Places API 返回提供的 API key 无效

标签 android google-places-api

通读关于这个主题的所有帖子并尝试所有组合: 1. 在进行 google places API 调用时使用了 android key ( 2.切换到浏览器键 3. 后来尝试使用服务器 key 。 4.重新生成 key 并尝试组合1-3。

没有任何效果!!我在 list 文件中的 Mapv2 API key 是 android key 。在我创建一个新项目并将我的包与新项目一起列出之前,这个应用程序一直在正常工作。我为这个包重新创建了新的 android key 。旧 key 仍然存在,但具有不同的项目。我没有删除旧项目,而是删除了它下面的所有 key 。 所以现在我在一个新项目下有了 android、浏览器的新 key 。 我做错了吗?

我收到错误消息“提供的 API key 无效”... 当我使用浏览器 key 从浏览器进行此调用时,它正在工作。不是不是来 self 的 android 应用程序。 有小费吗 ?。

请看下面的代码:-

final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place/nearbysearch";
final String OUT_JSON = "/json";
final String KEY=<browser-key>
final String SENSOR="false";
StringBuilder querystring = new StringBuilder(PLACES_API_BASE+OUT_JSON);
try{
querystring.append("?sensor="+SENSOR+"&key="+KEY);
String localquery="&location=37.316318,-122.005916&radius=500&name=traderjoe";
                querystring.append(URLEncoder.encode(localquery, "UTF-8"));
                URL url = new URL(querystring.toString());
                 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
         connection.setRequestMethod("GET");
           String line;
   StringBuilder builder = new StringBuilder();
   BufferedReader reader = new BufferedReader(
     new InputStreamReader(connection.getInputStream()));
   while((line = reader.readLine()) != null) {
    builder.append(line);
}
       System.out.println("JSON BUILDER INPUT FROM GOOGLE PLACES QUERY="+builder.toString());

这是我收到错误消息的地方:- Provided APi Key is invaild

最佳答案

这是我在 CSharp 中的 googlePlaceApi 代码

public List<GooglePlace> GoogleApiPlace(Coordinate startCoordinate)
    {
        List<GooglePlace> ListOfPlace = new List<GooglePlace> ();

        string apiURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=";
        string apiKey = "myKey";

        string linkApi = apiURL + startCoordinate.Latitude.ToString ().Replace (',', '.') + "," + startCoordinate.Longitude.ToString ().Replace (',', '.') + "&radius=1000&sensor=true&types=establishment&key=" + apiKey;

        var request = HttpWebRequest.Create(linkApi);
        request.ContentType = "application/xml";
        request.Method = "GET";

        try
        {
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Console.Out.WriteLine("Error fetching data, Server returned status code {0}", response.StatusCode);
                }
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    if (string.IsNullOrWhiteSpace(content))
                    {
                        Console.Out.WriteLine("Responsed contained empty body...");
                    }
                    else
                    {
                        XmlDocument document = new XmlDocument();
                        document.LoadXml(content);
                        Console.Out.WriteLine(content);
                        XmlNodeList nodeList = document.GetElementsByTagName("result");

                        foreach (XmlNode docNode in nodeList)
                        {
                            string tot = ((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText;
                            GooglePlace place = new GooglePlace()
                            {
                                Location = new Location()
                                {
                                    Coordinate = new Coordinate()
                                    {
                                        Latitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText, CultureInfo.InvariantCulture),
                                        Longitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lng")[0].InnerText, CultureInfo.InvariantCulture)
                                    }
                                },
                                Name = docNode.ChildNodes.Item(0).InnerText.TrimEnd().TrimStart(),
                                Vicinity = docNode.ChildNodes.Item(1).InnerText.TrimEnd().TrimStart()
                            };
                            ListOfPlace.Add(place);
                        }
                    }
                }
            }
        }
        catch
        {
            //TODO : Exception
        }

        return ListOfPlace;
    }

Api key 在这里 enter image description here

希望这对你有帮助;)

关于android - Google Places API 返回提供的 API key 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22905209/

相关文章:

Android NDK 不支持头文件?

android - 在虚拟机上运行Android进行开发

android - 默认描边并填充SVG

android - Google places API,获取用户地址

javascript - 加载时从谷歌地图中的字段获取值

html - Bootstrap Navbar 与 Google 重叠放置自动完成下拉菜单

ios - 如何从适用于 iOS 的 google place api 获取超过 20 个可能的地点

android - 以 viewModel 作为数据绑定(bind)变量重用 Fragments/Bottomsheets

android - placeautocompletefragment 完整地址

android - 内容提供商与服务