lat和長都具有當前位置的變量中,谷歌的地方搜索API JSON格式凌空響應全球動態數組列表不更新/函數修改而得到
if(lat!=0.0||longg!=0.0){
search();
details(placeId);
}
這裏是search()函數.... placeID是全球性的ArrayList的變量
public void search() {
String url ="https://maps.googleapis.com/maps/api/place/nearbysearch/json?Location="+lat+","+longg+"&radius=5000&type=hospital&name=Hospital&key=AIzaSyA71lHTBWnb8uNvUI0N8ch8lhvla_0pM8";
Log.e("URL 1st : ", url);
final ArrayList<String> idd = new ArrayList<>();
Response.Listener<JSONObject> myListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String status = null;
try {
status = response.getString("status");
} catch (JSONException e) {
e.printStackTrace();
}
if (status != null && status.equals("ZERO_RESULTS")) {
Log.e("Response Status: ", "search was successful but returned no results.This may occur if the search was passed a latlng in a remote location.");
Toast.makeText(NearByHospitals.this, "You are in remote location.\nNo Nearby Hospitals", Toast.LENGTH_SHORT).show();
} else if (status != null && status.equals("OVER_QUERY_LIMIT")) {
Log.e("Response Status: ", "indicates that you are over your quota.");
} else if (status != null && status.equals("REQUEST_DENIED")) {
Log.e("Response Status: ", "indicates that your request was denied, generally because of lack of an invalid key parameter.");
} else if (status != null && status.equals("INVALID_REQUEST")) {
Log.e("Response Status: ", "generally indicates that a required query parameter (location or radius) is missing.");
} else {
int sizeOfResponse = 0;
try {
sizeOfResponse = response.getJSONArray("results").length();
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < sizeOfResponse; i++) {
try {
if (response.getJSONArray("results").getJSONObject(i).has("opening_hours") && response.getJSONArray("results").getJSONObject(i).getJSONObject("opening_hours").getBoolean("open_now")) {
placeId.add(response.getJSONArray("results").getJSONObject(i).getString("place_id"));
Log.e("iddd", response.getJSONArray("results").getJSONObject(i).getString("place_id"));
locat.add(String.valueOf(response.getJSONArray("results").getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat")) + "," + String.valueOf(response.getJSONArray("results").getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
};
Response.ErrorListener myErrorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NearByHospitals.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
};
JsonObjectRequest myRequest = new JsonObjectRequest(Request.Method.GET,url, null, myListener, myErrorListener);
Volley.newRequestQueue(NearByHospitals.this).add(myRequest);
}
我想用placeId(全球ArrayList中)在我的個人資料作進一步的細節 這裏的功能是我的個人資料()函數
public void details(ArrayList<String> strId) {
for (int i = 0; i < strId.size(); i++) {
try {
String url2 = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + strId.get(i) + "&key=AIzaSyA718lHTBWnb8uNvUI0N8ch8lhvla_0pM8";
Log.e("URL2 ", url2);
Response.Listener<JSONObject> myListener2 = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String status = null;
try {
status = response.getString("status");
} catch (JSONException e) {
e.printStackTrace();
}
if (status != null && status.equals("ZERO_RESULTS")) {
Log.e("Response Status: ", "indicates that the reference was valid but no longer refers to a valid result. This may occur if the establishment is no longer in business.");
} else if (status != null && status.equals("OVER_QUERY_LIMIT")) {
Log.e("Response Status: ", "indicates that you are over your quota.");
} else if (status != null && status.equals("REQUEST_DENIED")) {
Log.e("Response Status: ", "indicates that your request was denied, generally because of lack of an invalid key parameter.");
} else if (status != null && status.equals("INVALID_REQUEST")) {
Log.e("Response Status: ", "generally indicates that the query (reference) is missing.");
} else {
if (status != null && status.equals("OK")) {
try {
hosNames.add(response.getJSONObject("result").getString("name"));
phoneNumbers.add(response.getJSONObject("result").getString("international_phone_number"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
};
Response.ErrorListener myErrorListener2 = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NearByHospitals.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
};
JsonObjectRequest myRequest2 = new JsonObjectRequest(Request.Method.GET, url2, null, myListener2, myErrorListener2);
Volley.newRequestQueue(NearByHospitals.this).add(myRequest2);
} catch (NullPointerException l) {
l.printStackTrace();
}
}
}
但問題是placeId是空的,它的大小爲零。我在調試時檢查這個變量n一切都很好arraylist在調試時被修改。我不知道我在哪裏錯了?
我實例化了onCreate()函數中的arraylist,並沒有得到任何錯誤。 –