Places API返回REQUEST_DENIED

时间:2013-05-24 23:24:13

标签: android google-places-api

我知道这上面有一百万个主题,但是我保证会在没有帮助的情况下搜索其中的许多主题。

我正在尝试使用Places API来返回结果。我尝试过以下方法:

确保一个有效的密钥(它在我使用mapFragment的同一个应用程序中工作,所以我知道密钥是好的。在生成密钥时启用了地点api。)。另外,我生成了一个新密钥并更新了应用程序,地图仍然有用。

确保正确形成搜索网址。我从日志中复制并粘贴到浏览器中,对浏览器密钥进行修改,结果也正确返回。

这是清单:

<permission
    android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.berrmal.locationbox.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIza**********************************Y" />

    <service
        android:name="com.berrmal.locationbox.LocationBoxService"
        android:enabled="true"
        android:exported="false" >
    </service>
</application>

这是AsyncTask:

public class PlacesProvider extends AsyncTask<String, Void, ArrayList<Place>> {
    //starting arg, progress update type, return from doInBack()
    public PlacesProvider() {

    }

    public ArrayList<Place> doInBackground(String... args) {
        //args[0] = search URL
        Log.d("lbServ","doing in background");
        ArrayList<Place> resultList = new ArrayList<Place>();
        DLJSON dLJSON = new DLJSON();
        try {
            resultList = dLJSON.getResults(args[0]);
        } catch (UnsupportedEncodingException uee) {
            Log.d("lbServ","unsupp encode ex");
        } catch (IOException ioe) {
            Log.d("lbServ","IO exception");
        } catch (IllegalStateException ils) {
            Log.d("lbserv","iill state exception");
            ils.printStackTrace();
        } catch (Exception ex) {
            Log.d("lbServ", "unknown exception in dlJson");
        }
        if (resultList == null) {
            Log.d("lbServ","resultList is null");
        }
        return resultList;
    }

    protected void onPostExecute(ArrayList<Place> result) {
        Log.d("lbServ","onnPostExecute");
        placesList = result;
        //TODO do something with the result, i.e. send it to MainActivity via instance that bound to server
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        //resultIntent.setComponent(new ComponentName("com.berrmal.locationbox", "MainActivity"));
        resultIntent.putExtra("SearchSuccess", true);
        resultIntent.putExtra("searchID", 1);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(resultIntent);
        Log.d("lbServ","service suicide");
        stopSelf();
    }

JSON查询器/解析器

public ArrayList<Place> getResults(String searchURL) throws UnsupportedEncodingException, IOException, IllegalStateException{
    try {
        url = new URL(searchURL);
        Log.d("lbDLJSON",url.toString());
    } catch (Exception ex) {
        Log.d("lbDLJSON", "malformed URL");
    }

    try {
            urlConn = (HttpsURLConnection) url.openConnection();
            Log.d("dlJson","url connection open");
            is = new BufferedInputStream(urlConn.getInputStream());
            Log.d("dljson","buffered stream open");
    } catch (IOException ioe) {
            Log.d("lbDLJSON", "io exception");
    } catch (Exception e) {
        Log.d("lbDLJSON", "that other exception");
    } 
    jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    Log.d("lbjson","json and inputstream readers exists");
    resultList = new ArrayList<Place>();
    jsonReader.beginObject();
    Log.d("lbjson", "top level object open");
    while (jsonReader.hasNext()) {
        String name = jsonReader.nextName().toString();
        if (name.equals("results")) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()); {
                //resultList.add(placeBuilder(jsonReader));
                Log.d("lbjson", "results were null");
            }
            jsonReader.endArray();
        } else if (name.equals("status")){
            statusCode = jsonReader.nextString().toString();
            Log.d("dljson", "status code: " + statusCode);
        } else {
            Log.d("lbjson", "skipped " + name);
            jsonReader.skipValue();
        }

    }
    jsonReader.endObject();
    Log.d("lbDLJSON", "top level JSON object closed");
    jsonReader.close();
    urlConn.disconnect();

    return resultList;
    //Log.d("lbjson", "");

}

LogCat输出:

05-24 16:04:07.859: D/lbServ(19523): search
05-24 16:04:07.859: D/lbServ(19523): executeAsyncTask
05-24 16:04:07.859: D/lbServ(19523): doing in background
05-24 16:04:07.859: D/lbDLJSON(19523): https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIza***************************Y&location=34.0937751,-117.6730797&radius=15000&sensor=true&keyword=food
05-24 16:04:07.859: D/dlJson(19523): url connection open
05-24 16:04:08.199: D/dljson(19523): buffered stream open
05-24 16:04:08.199: D/lbjson(19523): json and inputstream readers exists
05-24 16:04:08.199: D/lbjson(19523): top level object open
05-24 16:04:08.199: D/lbjson(19523): skipped html_attributions
05-24 16:04:08.199: D/lbjson(19523): results were null
05-24 16:04:08.199: D/dljson(19523): status code: REQUEST_DENIED
05-24 16:04:08.199: D/lbDLJSON(19523): top level JSON object closed
05-24 16:04:08.199: D/lbServ(19523): onnPostExecute
05-24 16:04:08.220: D/lbServ(19523): service suicide

我可以发布网址生成方法,但正如您所见,URL是从日志中正确形成的。

我一直在谷歌搜索和尝试几个小时,所以我真的很难过。我不明白为什么我可以将相同的链接粘贴到浏览器中,将密钥更改为浏览器密钥,并获得有效的结果(8.3kb),但此处的应用程序返回以下85b结果:

{
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}

点击“试试看”后,我得到同样的请求拒绝结果在api控制台服务列表上链接,但如果我手动创建URL,我会得到有效的结果......?

修改

我在清单中保留了android app密钥,对于maps权限,但是在HTTP请求中,我将密钥更改为浏览器密钥,并在places查询中获得了成功的结果。我不知道为什么,它真的没有意义。我有点害怕它违反了服务条款,但我不太确定。这是否表明我做错了什么?或者有没有人知道联系谷歌报告它的方法?或者我是否应该一直使用这两个键?

1 个答案:

答案 0 :(得分:1)

好的,我可以说,问题是通过使用两个键来解决的。我在清单中使用Android API密钥,这使mapFragment正常工作。

让地点请求回来&#34; OK&#34;而不是&#34; REQUEST_DENIED&#34;,我不得不使用Google API控制台中的浏览器密钥。我假设,因为我的应用构建了一个搜索网址并使用HTTP连接发送和接收,谷歌的服务器认为它是一个浏览器而不是一个应用程序。我不知道这是否正确用法,如果我从谷歌那里得到一个令人讨厌的内容,我会更新帖子。