好的,我不确定我是否在问正确的问题,但这是我能想到的解决问题的唯一方法。
我必须调用 API 来获取需要在此方法中使用的信息。
public void setBuses(JSONArray theBuses)
{
try {
for(int x = 0; x < theBuses.length() ; x++)
{
busExists = false;
if(x >= buses.size())
{
currentBus = new Bus();
}
else
{
busExists = true;
currentBus = buses.get(x);
currentMarker = busMarkers.get(x);
}
if(theBuses.getJSONObject(x).has("name"))
{
currentBus.setName(theBuses.getJSONObject(x).getString("name"));
}
if(theBuses.getJSONObject(x).has("driver"))
{
currentBus.setDriver(theBuses.getJSONObject(x).getString("driver"));
}
if(theBuses.getJSONObject(x).has("route"))
{
currentBus.setRoute(theBuses.getJSONObject(x).getString("route"));
}
if(theBuses.getJSONObject(x).has("active"))
{
currentBus.setActive(theBuses.getJSONObject(x).getBoolean("active"));
}
if(theBuses.getJSONObject(x).has("lastStop"))
{
currentBus.setLastStop(theBuses.getJSONObject(x).getString("lastStop"));
}
if(theBuses.getJSONObject(x).has("lastLong"))
{
currentBus.setLongi(theBuses.getJSONObject(x).getDouble("lastLong"));
}
if(theBuses.getJSONObject(x).has("lastLat")) {
currentBus.setLat(theBuses.getJSONObject(x).getDouble("lastLat"));
}
if(currentMarker != null)
{
currentMarker.remove();
}
if(currentBus.isActive())
{
BusStop nextStop = routes.getNextStop(currentBus.getLastStop(), currentBus.getRoute());
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(5000);
client.get("https://maps.googleapis.com/maps/api/directions/json?origin="+currentBus.getLat()+"," +
""+currentBus.getLongi()+"&destination="+nextStop.getLat()+","+nextStop.getLongi()+"&departure_time=1541202457&traffic_model=best_guess&key=AIzaSyCADdN-VW0vFCKz4uWqdL97Idk8ezENfHk"
, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject distanceObject) {
setBusHelper(distanceObject);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
});
}
if(busExists) //this needs to be moved to the helper function and the varibles x, currentBus, mMap, and nextStop need to be passed to that function
{
Bus temp = buses.set(x,currentBus);
Marker tempM = busMarkers.set(x,currentMarker);
}
else
{
buses.add(currentBus);
busMarkers.add(currentMarker);
}
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
google 的 pull 调用这个方法从 API 中获取数据
private void setBusHelper(JSONObject distanceObject) {
String timeToNextStop = "Unknown";
try {
timeToNextStop = distanceObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("duration").getString("text");
currentMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(currentBus.getLat(), currentBus.getLongi())).title("Time to Next Stop:" + timeToNextStop).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
} catch (JSONException e) {
e.printStackTrace();
}
}
我遇到的问题是因为调用是 aSync 我没有标记存储在我的数组中。我需要能够将添加标记和数组存储所需的信息传递给 JSONhandler 中的 onSuccess 方法。我不知道怎么做,我在网上找不到任何例子。您可以提供的任何帮助将不胜感激。
我会提到我曾尝试使用 SyncHttpClient 但它似乎不能同步工作。