Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 请求被拒绝,Google Places API密钥用于Android应用程序中使用的服务器Web_Java_Android_Google Places Api - Fatal编程技术网

Java 请求被拒绝,Google Places API密钥用于Android应用程序中使用的服务器Web

Java 请求被拒绝,Google Places API密钥用于Android应用程序中使用的服务器Web,java,android,google-places-api,Java,Android,Google Places Api,我正在尝试从Google Places的API获取Android应用程序的信息。要做到这一点,首先我在我的谷歌帐户中启用了这个API 其次,我为浏览器创建了一个API密钥。由于另一个API,我已经有一个API密钥服务器 所以,在我的代码中,我已经用这两个键进行了测试,并且用这两个键我总是得到相同的结果 { “错误消息”:“此服务需要API密钥。” “html_属性”:[] “结果”:[] “状态”:“请求被拒绝” } 我用来打电话的代码是 @Override pro

我正在尝试从Google Places的API获取Android应用程序的信息。要做到这一点,首先我在我的谷歌帐户中启用了这个API

其次,我为浏览器创建了一个API密钥。由于另一个API,我已经有一个API密钥服务器

所以,在我的代码中,我已经用这两个键进行了测试,并且用这两个键我总是得到相同的结果

{

“错误消息”:“此服务需要API密钥。”

“html_属性”:[]

“结果”:[]

“状态”:“请求被拒绝”

}

我用来打电话的代码是

        @Override
    protected String doInBackground(LocationService... ls) {
        JSONObject result = new JSONObject();
        URL url;
        HttpsURLConnection urlConnection;

        // Making HTTP request
        try {

            //Define connection
            url = new URL("https://maps.googleapis.com/maps/api/place/nearbysearch/json");
            urlConnection = (HttpsURLConnection)url.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("charset", "utf-8");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setUseCaches(false);

            //Send data
            String parameters = "?location=" + String.valueOf(ls[0].getLocation().getLatitude()) + "," + String.valueOf(ls[0].getLocation().getLongitude());
            parameters+="&radius=5000";
            parameters+="&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket";
            parameters+="&sensor=false";
            parameters+="&key=" + Constants.API_KEY_BROWSER_APPLICATIONS;
            byte[] postData = parameters.getBytes(Charset.forName("UTF-8"));
            int postDataLength = postData.length;
            urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            DataOutputStream data = new DataOutputStream(urlConnection.getOutputStream());
            data.write(postData);
            data.flush();
            data.close();

            Log.d(TAG, "Datos enviados");
            Log.d(TAG, "ResponseCode: " + String.valueOf(urlConnection.getResponseCode()));

            //Display what returns POST request

            StringBuilder sb = new StringBuilder();

            int HttpResult = urlConnection.getResponseCode();

            if(HttpResult == HttpURLConnection.HTTP_OK){
                String json;

                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                String line;

                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }

                br.close();

                //System.out.println(""+sb.toString());
                Log.d(TAG, "json: " + sb.toString());

                FileService file = new FileService();
                file.writeLog(POIActivity.TAG, getClass().getName(), POIActivity.urlConnection + parameters);
                file.writeLog(POIActivity.TAG, "doInBackground", sb.toString());

                // Parse the String to a JSON Object
                result = new JSONObject(sb.toString());

            }else{
                //System.out.println(urlConnection.getResponseMessage());
                Log.d(TAG, "urlConnection.getResponseMessage(): " + urlConnection.getResponseMessage());
                result = null;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.d(TAG, "UnsuppoertedEncodingException: " + e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "Error JSONException: " + e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "IOException: " + e.toString());
        }

        // Return JSON Object
        return result.toString();


    }
当我调用API时,我得到了ResponseCode=200,我构建的调用最终是这样的

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=38.26790166666667,-0.7052183333333333&radius=5000&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket&sensor=false&key=API_KEY
记住,像API_KEY一样,我同时使用了服务器应用程序的API KEY和浏览器应用程序的API KEY,这两种方法的结果都是一样的


真诚地说,我对这个问题感到绝望,因为我不知道我做错了什么

来自文档:

注意:您需要安卓API密钥,而不是浏览器密钥。您可以对Google Maps Android API v2应用程序和Google Places API for Android应用程序使用相同的API密钥


查看更多帮助。

有关更简单的方法,请尝试最新的GCM配置文件实现,并使用开发人员界面轻松创建项目


问题在于,您没有使用Android版的
Google Places API
,而是使用
Google Places API Web服务

是使用Android的Google Places API的示例,也是使用Google Places API Web服务的示例。你肯定在用后者

启用
Google Places API Web服务
,它将工作:

如果您在登录到Google云控制台帐户时转到此链接:

这是应启用的API:


谢谢,但是。。。我刚刚用Android API密钥进行了测试,但不起作用:(谢谢@Daniel Nugent!!!我已经启用了我的Google Places API Web服务,但…仍然不起作用!!!我认为你是对的,我必须使用Google Places API Web服务,可能我的代码中有错误。为了进行测试,我使用了API密钥服务器和API密钥浏览器请注意,文档中说你需要使用服务器Key,但我已经将其与浏览器密钥一起使用,并且也有效。以下是文档:Yeees!!!我已在浏览器上使用API密钥服务器启动此url,并且有效!!!但是,如果我尝试通过Android应用程序获取信息,则无效!!!:(错误相同:{“错误消息”:“此服务需要API密钥。”,“html_Attributes”:[],“results”:[],“status”:“REQUEST_DENIED”}已解决!!!我已修改我的方法从Google Places API Web服务下载数据,您推荐的方法救了我的命!!非常感谢@Daniel Nugent!!!