与谷歌的地方API密钥在一个Android应用程序中使用服务器W

与谷歌的地方API密钥在一个Android应用程序中使用服务器W

本文介绍了请求被拒绝与谷歌的地方API密钥在一个Android应用程序中使用服务器Web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个Android应用程序从谷歌的地方API的信息。为了做到这一点,首先我已经启用了这个API在我的谷歌帐户。

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

所以,在我的code我一直在测试这两个按键以及带我有总是相同的结果!

这是我使用拨打电话的code是...

  @覆盖
    保护字符串doInBackground(LocationService ... LS){
        JSONObject的结果=新的JSONObject();
        URL网址;
        HttpsURLConnection URLConnection的;

        //使HTTP请求
        尝试 {

            //定义连接
            URL =新的URL(https://maps.googleapis.com/maps/api/place/nearbysearch/json);
            的URLConnection =(HttpsURLConnection)url.openConnection();

            urlConnection.setRequestMethod(POST);
            urlConnection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
            urlConnection.setRequestProperty(字符集,UTF-8);
            urlConnection.setRequestProperty(接受,应用/ JSON);
            urlConnection.setDoOutput(真正的);
            urlConnection.setDoInput(真正的);
            urlConnection.setUseCaches(假);

            //发送数据
            ?位置=字符串参数= +将String.valueOf(LS [0] .getLocation()getLatitude())+,+将String.valueOf(LS [0] .getLocation()getLongitude());
            参数+ =与&半径= 5000;
            parameters+="&types=restaurant|health|city_hall|gas_station|shopping_mall|grocery_or_supermarket";
            参数+ =&放大器;传感器=假;
            参数+ =&放大器;关键=+ Constants.API_KEY_BROWSER_APPLICATIONS;
            byte []的POSTDATA = parameters.getBytes(Charset.forName(UTF-8));
            INT postDataLength = postData.length;
            urlConnection.setRequestProperty(内容长度,Integer.toString(postDataLength));
            DataOutputStream类数据=新DataOutputStream类(urlConnection.getOutputStream());
            data.write(POSTDATA);
            data.flush();
            data.close();

            Log.d(TAG,DATOS enviados);
            Log.d(TAG,响应code:+将String.valueOf(urlConnection.getResponse code()));

            //显示的内容返回POST请求

            StringBuilder的SB =新的StringBuilder();

            INT型Htt presult = urlConnection.getResponse code();

            如果(Htt的presult == HttpURLConnection.HTTP_OK){
                JSON字符串;

                的BufferedReader BR =新的BufferedReader(新的InputStreamReader(urlConnection.getInputStream(),UTF-8));

                串线;

                而((行= br.readLine())!= NULL){
                    sb.append(行+\ N);
                }

                br.close();

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

                的FileService文件=新的FileService();
                file.writeLog(POIActivity.TAG,的getClass()的getName(),POIActivity.urlConnection +的参数。);
                file.writeLog(POIActivity.TAGdoInBackground,sb.toString());

                //解析字符串到一个JSON对象
                结果=新的JSONObject(sb.toString());

            }其他{
                //System.out.println(urlConnection.getResponseMessage());
                Log.d(TAG,urlConnection.getResponseMessage():+ urlConnection.getResponseMessage());
                结果= NULL;
            }

        }赶上(UnsupportedEncodingException E){
            e.printStackTrace();
            Log.d(TAG,UnsuppoertedEncodingException:+ e.toString());
        }赶上(JSONException E){
            e.printStackTrace();
            Log.d(TAG,错误JSONException:+ e.toString());
        }赶上(IOException异常E){
            e.printStackTrace();
            Log.d(TAG,IOException异常:+ e.toString());
        }

        //返回JSON对象
        返回result.toString();


    }
 

当我打这个电话的API我有样反应code = 200,呼叫,我打造的是最后这样的...

<$p$p><$c$c>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密钥的服务器应用程序和API密钥的浏览器应用程序,我已经得到了相同的结果与两个。

真诚的,我绝望了这个问题,因为我不知道我做错了!

解决方案

现在的问题是,你不使用Android版的谷歌的地方API,您使用的是谷歌的地方API Web服务

Here是使用谷歌API的地方为Android 和here是使用的例子谷歌的地方API Web服务。你肯定是使用了后者。

启用谷歌的地方API Web服务,它会工作:

I'm trying to get info from the API of Google Places for an Android application. To do that, first I have enabled this API in my Google Account.

Second, I have created an API KEY for Browser. I already have an API KEY Server due to another API.

So, in my code I have been tested with these two Keys and with both I've got always the same result!!!

The code that I'm using to make the call are ...

        @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();


    }

When I make the call to the API I've got like ResponseCode = 200 and the call that I build is finally like that ...

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

Remember, like API_KEY I have used both, Api Key for server applications and Api Key for browser applications and I've got the same result with both.

Sincerely, I'm desperate with this problem because I don't know what I am doing wrong!!!

解决方案

The problem is that you are not using the Google Places API for Android, you are using the Google Places API Web Service.

Here is an example of using Google Places API for Android, and here is an example of using the Google Places API Web Service. You are definitely using the latter.

Enable the Google Places API Web Service and it will work:

这篇关于请求被拒绝与谷歌的地方API密钥在一个Android应用程序中使用服务器Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:03