API调用在java项目中有效,但在Android应用程序中调用时无效
我们正在尝试使用GooglePlacesAPI来查找附近的位置,并将其作为java项目运行。但是,在android应用程序中的按钮的onClick()函数下运行相同的代码会导致从Google返回的JSON字符串为空。硬编码GPS坐标用于这两种情况 为什么呢 这是在尝试获取JSON数据时显示的错误API调用在java项目中有效,但在Android应用程序中调用时无效,java,android,json,google-places-api,Java,Android,Json,Google Places Api,我们正在尝试使用GooglePlacesAPI来查找附近的位置,并将其作为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
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。我们已包括必要的位置和互联网权限
谢谢 将aqueryjar添加到您的项目中,然后尝试以下代码
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();
}
}
});
纬度=18;
经度=50;
StringBuilder=新的StringBuilder(“https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
builder.append(“location=“+纬度+”,“+经度”);
builder.append(“&radius=100”);
builder.append(“&sensor=true”);
builder.append(“&types=“+type”);
builder.append(“&key=xxxxxxxxxxxxxxxxxxxxxx”);
字符串url=builder.toString();
ajax(url,JSONObject.class,新的AjaxCallback()){
@凌驾
公共无效回调(字符串url、JSONObject对象、,
AjaxStatus(状态){
//TODO自动生成的方法存根
super.callback(url、对象、状态);
//System.out.println(“objectttt==”+object);
JSONArray JSONArray;
试一试{
jsonArray=object.getJSONArray(“结果”);
对于(int j=0;jI)来说,这是个问题,但建议(在更高版本中是必需的)在后台线程上执行网络操作,例如使用AsyncTask。即使它不能解决此问题,这也是一件好事。这是如何工作的?您知道任何使用AsyncTask的教程吗?
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();
}
}
});