【问题标题】:Overwriting the JsonHttpResponseHandler class to add a param pass to onSuccess覆盖 JsonHttpResponseHandler 类以向 onSuccess 添加参数传递
【发布时间】:2016-10-26 20:52:30
【问题描述】:

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

我必须调用 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,但它似乎无法同步工作。

【问题讨论】:

    标签: java android google-maps asynchttpclient jsonresponse


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 2021-08-15
      • 1970-01-01
      • 2015-12-20
      • 2011-02-13
      • 1970-01-01
      相关资源
      最近更新 更多