緯度と長い変数は、現在位置があるGoogleの場所検索APIからJSON形式でボレー応答を取得中に関数に変更しないで、ここでグローバル動的ArrayListのは、更新/
if(lat!=0.0||longg!=0.0){
search();
details(placeId);
}
検索()関数.... placeIDでありますグローバルArrayListのは、私がここに詳細 のための私の詳細機能に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);
}
であることは(私の詳細である)機能
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();
}
}
}
問題はありますが、場所IDは空でサイズはゼロです。私はデバッグ中にこの変数をチェックします。nすべてはうまくいきます。arraylistはデバッグ中に変更されました。私は知らないどこが間違っているのですか?
私はonCreate()関数でarraylistsをインスタンス化し、エラーは発生しませんでした。 –