我知道这方面有一百万个线程,但我在没有帮助的情况下搜索了很多线程,我保证。
我正在尝试使用 Places API 返回结果。我尝试了以下方法:
确保一个有效的密钥(它在我使用 mapFragment 的同一个应用程序中工作,所以我知道密钥是好的。在生成密钥时启用了 places api。)。此外,我生成了一个新密钥并更新了应用程序,地图仍然有效。
确保搜索 URL 格式正确。我从日志中复制并粘贴到浏览器中,用浏览器键替换,结果正确返回。
这是清单:
<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>
这是异步任务:
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 生成方法,但正如您所见,URL 是从日志中正确形成的。
我已经在谷歌上搜索并尝试了几个小时,所以我真的很难过。我不明白为什么我可以将相同的链接粘贴到浏览器中,将密钥更改为浏览器密钥,并获得有效结果(8.3kb),但是这里的应用程序返回以下 85b 结果:
{
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}
单击“尝试一下!”时,我得到了同样的请求被拒绝结果。服务的 api 控制台列表上的链接,但是如果我手动创建 URL,我会得到有效的结果......?
编辑:
我将 android app 密钥保留在清单中,以获得地图权限,但在 HTTP 请求中,我将密钥更改为浏览器密钥,并在地点查询中获得了成功的结果。我不知道为什么,这真的没有意义。我有点担心它违反了 TOS,但我不太确定。这是否表明我做错了什么?或者有谁知道联系谷歌举报的方法吗?还是我应该一直使用这两个键?