我们正在尝试使用Google Places API查找附近的地方,并在仅作为Java项目运行时使其正常工作。但是,在Android应用程序内的按钮的onClick()函数下运行相同的代码会导致从Google返回的JSON字符串为空。硬编码的GPS坐标用于两者。
为什么?
这是尝试获取JSON数据时显示的错误
05-14 00:54:12.828: E/PlacesService(8677): org.json.JSONException: End of input at character 0 of
05-14 00:54:12.828: E/PlacesService(8677): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-14 00:54:12.828: E/PlacesService(8677): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
这是进行API调用的地方:
private String makeUrl(double latitude, double longitude, String place) {
StringBuilder urlString = new StringBuilder(
"https://maps.googleapis.com/maps/api/place/search/json?");
if (place.equals("")) {
urlString.append("&location=");
urlString.append(Double.toString(latitude));
urlString.append(",");
urlString.append(Double.toString(longitude));
urlString.append("&radius=1000");
// urlString.append("&types="+place);
urlString.append("&sensor=false&key=" + API_KEY);
} else {
urlString.append("?types=" + "bar" + "|" + "night_club");
urlString.append("&location=");
urlString.append(Double.toString(latitude));
urlString.append(",");
urlString.append(Double.toString(longitude));
urlString.append("&radius=1000");
urlString.append("&sensor=false&key=" + API_KEY);
}
return urlString.toString();
}
protected String getJSON(String url) {
return getUrlContents(url);
}
private String getUrlContents(String theUrl) {
StringBuilder content = new StringBuilder();
try {
URL url = new URL(theUrl);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()), 8);
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
}catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
以下是我们如何调用上述方法来获取JSON字符串:
String urlString = makeUrl(32.8400, -117.2769 placeSpacification);
String json = getJSON(urlString);
这是我们的AndroidManifest.xml。我们已经包含了必要的位置和Internet权限。
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testapp.ListViewAndroidExample"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
谢谢!
答案 0 :(得分:0)
嘿,将Aquery jar添加到您的项目中并尝试使用代码。
latitude =18 ;
longitude = 50 ;
StringBuilder builder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
builder.append("location="+latitude+","+longitude);
builder.append("&radius=100");
builder.append("&sensor=true");
builder.append("&types="+type);
builder.append("&key=XXXXXXXXXXXXXXXXXXXXX");
String url = builder.toString();
aQuery.progress(progressDialog).ajax(url, JSONObject.class, new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject object,
AjaxStatus status) {
// TODO Auto-generated method stub
super.callback(url, object, status);
//System.out.println("objectttt ==== "+object);
JSONArray jsonArray;
try {
jsonArray = object.getJSONArray("results");
for(int j=0; j<jsonArray.length(); j++)
{
JSONObject jsonObject = jsonArray.getJSONObject(j);
String placeName = jsonObject.getString("name");
String placeAddress= jsonObject.getString("vicinity");
String image=jsonObject.getString("icon");
JSONObject jsonObject2 = jsonObject.getJSONObject("geometry");
JSONObject jsonObject3 =jsonObject2.getJSONObject("location");
for(int i=0 ;i<jsonObject3.length();i++)
{
PlaceLatitude =jsonObject3.getDouble("lat");
PlaceLongitude =jsonObject3.getDouble("lng");
}
try
{
imgURL= new URL(image);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
arrPlaces.add(placeName);
arrPlaceImage.add(imgURL);
arrPlaceAddress.add(placeAddress);
arrPlaceLatitude.add(PlaceLatitude);
arrPlaceLogitude.add(PlaceLongitude);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});