我知道这上面有上百万个线程,但我保证,我在没有任何帮助的情况下搜索了其中的许多线程。
我试图使用Places返回结果。我尝试了以下几点:
确保一个有效的密钥(在我使用mapFragment的同一个应用程序中,它可以工作,所以我知道密钥是好的。)在生成密钥时启用了places。此外,我生成了一个新的键和更新的应用程序,地图仍然工作。
确保搜索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>下面是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生成方法,但是正如您所看到的,URL是从日志中适当形成的。
我在谷歌上搜索并尝试了好几个小时,所以我真的很困惑。我不明白为什么我可以将相同的链接粘贴到浏览器中,将键更改为浏览器键,并获得有效的结果(8.3kb),但是这里的应用程序返回以下85b结果:
{
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}当我点击“试试看!”时,同样的请求被拒绝。链接到服务的api控制台列表上,但是如果我手动创建URL,就会得到有效的结果.?
编辑:
我将android应用键保留在清单中,以获得映射权限,但在HTTP请求中,我将键更改为浏览器键,并在places中获得了成功的结果。我不知道为什么,这不太合理。我有点担心这违反了TOS,但我不太确定。这是否表明我做错了什么?或者有人知道如何联系谷歌来报告这件事?还是我应该一直用这两把钥匙?
发布于 2013-05-28 21:54:26
好的,据我所知,这个问题是通过使用两个键来解决的。我在清单中使用了Android键,这可以使mapFragment正常工作。
为了让places请求返回"OK“而不是"REQUEST_DENIED",我必须使用Google控制台中的浏览器键。我猜想,因为我的应用程序构建了一个搜索URL,并且使用HTTP连接发送和接收,所以google的服务器认为它是一个浏览器而不是一个应用程序。我不知道这是否正确的用法,如果我从谷歌得到一个nastygram,我会更新线程。
https://stackoverflow.com/questions/16744861
复制相似问题