覆盖JsonHttpResponseHandler类以向onSuccess添加param传递

时间:2016-10-26 20:52:31

标签: java android google-maps asynchttpclient jsonresponse

好吧我不确定我是否在问正确的问题,但这是我能想到解决问题的唯一方法。

我必须调用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();

    }
}

谷歌的拉动调用此方法从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但它似乎不能同步工作。

1 个答案:

答案 0 :(得分:0)

好的,所以答案是根本不使用AsyncHttpClient。我使用AsyncTask来调用Apaches HTTP客户端,它将所有内容保存在同一个线程中并允许我进行多次调用。程序现在很好用。