Android - 等待截击响应以继续

Android - Wait for volley response for continue

我构建了一个应用程序,它在地图中加载标记,我为 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 步:创建此界面

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);
                });