Android - 等待续播响应继续

时间:2018-03-17 23:12:09

标签: java android geofire

我构建了一个应用程序,它在地图中加载标记,标记我为volley JSON文件获取它,但我首先需要加载volley,并在继续执行代码后,因为另一种方式显示我的错误 latLng null ,此参数未加载快速,另一个方法先执行并显示为null。

我的加载标记的截击码

public  void getMarkers(){

    requestQueue = Volley.newRequestQueue(getApplicationContext());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {


        @Override
        public void onResponse(JSONObject response) {

            try {

                JSONArray saagTracker = response.getJSONArray("saagMRK");
                for (int i = 0; i < saagTracker.length(); i++) {
                    JSONObject object = saagTracker.getJSONObject(i);

                    title = object.getString(TITLE);
                    snnipet = object.getString(SNNIP);
                    latLng = new LatLng(Double.parseDouble(object.getString(LAT)), Double.parseDouble(object.getString(LNG)));
                    coor = coor + "|" + object.getString(LAT) + "," + object.getString(LNG);                        // Menambah data marker untuk di tampilkan ke google map

                    addMarker(latLng, title, snnipet);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //JSONArray array = new JSONArray(response.body().string());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this, "Ocurrio un error", Toast.LENGTH_LONG).show();
        }
    });
    requestQueue.add(jsonObjectRequest);
}

onCreate首先需要latLng才能加载我的geofire参数

geoQuery = geofire.queryAtLocation(new GeoLocation(latLng.latitude, latLng.longitude),0.1f); // latLng it coming null

1 个答案:

答案 0 :(得分:2)

您需要为排球响应实施回调。一个非常简单的方法是:

第1步:创建此界面

public interface VolleyCallBack {
    void onSuccess();
}

第2步:将getMarkers()方法更改为:

public  void getMarkers(final VolleyCallBack callBack){

requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new Response.Listener<JSONObject>() {


    @Override
    public void onResponse(JSONObject response) {

        try {

            JSONArray saagTracker = response.getJSONArray("saagMRK");
            for (int i = 0; i < saagTracker.length(); i++) {
                JSONObject object = saagTracker.getJSONObject(i);

                title = object.getString(TITLE);
                snnipet = object.getString(SNNIP);
                latLng = new LatLng(Double.parseDouble(object.getString(LAT)), Double.parseDouble(object.getString(LNG)));
                coor = coor + "|" + object.getString(LAT) + "," + object.getString(LNG);                        // Menambah data marker untuk di tampilkan ke google map

                addMarker(latLng, title, snnipet);

                callback.onSuccess();

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //JSONArray array = new JSONArray(response.body().string());

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(MainActivity.this, "Ocurrio un error", Toast.LENGTH_LONG).show();
    }
});
requestQueue.add(jsonObjectRequest);
}

第3步:像这样调用getMarkers()方法:

getMarkers(new VolleyCallBack() {

                @Override
                public void onSuccess() {
                    // this is where you will call the geofire, here you have the response from the volley.
                    geoQuery = geofire.queryAtLocation(new GeoLocation(latLng.latitude, latLng.longitude),0.1f);
                });