2017-12-02 171 views
0

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在调试时被修改。我不知道我在哪里错了?

+0

我实例化了onCreate()函数中的arraylist,并没有得到任何错误。 –

回答

0

从下面:

search(); 
details(placeId); 

在方法搜索()你是一个异步线程修改placeId列表,这样,当你运行的详细信息(placeId),要传递作为参数placeId不算不保证修改列表。您可能想要在填充placeId内的抽签响应中调用详细信息(placeId)。

+0

明白了......谢谢......我在search()的末尾调用了details()并将placeID作为参数传递。 –

+0

很高兴我能帮到你。如果有帮助,请接受答案。 – HaroldSer

+0

我想将您的建议标记为答案,但我的代表太低,无法投票回答:(再次感谢 –